动态数组成员变量

时间:2011-02-13 15:49:40

标签: c++

嘿,我是c ++的新手,并且仍然在研究它的特点。我正在努力弄清楚这段代码出了什么问题。我已经介入它,一切都正确计算。问题是,一旦派生类Calculate函数结束,基类中的value_array似乎不会保留值。我想我已经正确地声明和分配了数组。我很难过......

#include <iostream>

class Indicator 
{
protected:
    double * value_array;
    double * input_array;
    int input_size;
public:
    Indicator(double input[], int size) 
    { 
        input_array = input; 
        input_size = size; 
        value_array = new double[size]; // issue with value_array
    }
    double operator[] (int index) { return value_array[index]; }
    void virtual Calculate() {}
    ~Indicator() { delete[] value_array; }
};

class SMA : public Indicator 
{
private:
    int nperiod;
    double sum;
public:
    SMA(double input[], int size, int period) : Indicator(input, size) 
    { 
        nperiod = period; 
        sum = 0; 
        Calculate(); 
    }
    void Calculate();
};

void SMA::Calculate()
{ 
    for (int i=0; i<input_size; i++)
    {
        if (i > nperiod - 1) 
        {
            sum += input_array[i] - input_array[i-nperiod];
            value_array[i] = sum / nperiod;
        }
        else
        {
            sum += input_array[i];
            value_array[i] = sum / (i+1);
        }
    }
}

int main(int argc, const char *argv[]) {
    double input[] = {1,2,3,4,5,6,7,8,9,10};
    Indicator indicator = SMA(input,10,5);

    double value = indicator[0];
    std::cout << "value: " << value << std::endl;

    std::cin.get();
    exit(0);
}

更新

这是用向量实现的代码。我希望将输入保留为double []以与其他库保持一致,以及我应该注意的任何其他潜在问题?

#include <iostream>
#include <vector>

class Indicator 
{
protected:
    std::vector<double> value_vector;
    double * input_array;
    int input_size;
public:
    Indicator(double input[], int size) 
    { 
        input_array = input; 
        input_size = size; 
        value_vector.reserve(size);
    }
    double operator[] (int index) { return value_vector[index]; }
    void virtual Calculate() {}
};

class SMA : public Indicator 
{
private:
    int nperiod;
    double sum;
public:
    SMA(double input[], int size, int period) : Indicator(input, size) 
    { 
        nperiod = period; 
        sum = 0; 
        Calculate(); 
    }
    void Calculate();
};

void SMA::Calculate()
{ 
    for (int i=0; i<input_size; i++)
    {
        if (i > nperiod - 1) 
        {
            sum += input_array[i] - input_array[i-nperiod];
            value_vector.push_back(sum / nperiod);
        }
        else
        {
            sum += input_array[i];
            value_vector.push_back(sum / (i+1));
        }
        std::cout << "sma:  " << value_vector[i] << std::endl;
    }
}

int main(int argc, const char *argv[]) {
    double input[] = {1,2,3,4,5,6,7,8,9,10};
    Indicator indicator = SMA(input,10,5);

    for (int i=0; i<10; i++)
    {
        std::cout << "main: " << indicator[i] << std::endl;
    }

    std::cin.get();
    exit(0);
}

3 个答案:

答案 0 :(得分:3)

那是因为你违反了Rule of Three。由于您的类管理资源,因此它需要复制构造函数和赋值运算符。我强烈建议您使用T*数据成员替换任何std::vector<T>数据成员。那么您不需要手动编写这些特殊的成员函数。

答案 1 :(得分:2)

Hia的,

有些事情是错的。

正如FredOverflow所说,你需要一个复制构造函数和赋值,如:

Indicator::Indicator(const Indicator& other)
{
  input_size = other.input_size;

  //direct copy of reference as indicator doesn't own this data
  //Note a shared pointer (such as boost::shared_ptr) would be better than a naked reference
  input_array = other.input_array; 
  //construct a new set of data
  value_array = new double[input_size]; 
  //do you want to copy the data too? maybe a memcpy follows?
  memcpy(value_array, other.value_array, input_size*sizeof(double));
}

然后你需要一个作业

Indicator&
Indicator::operator=(const Indicator& other)
{
  //make sure you are not assigning itself
  if(this != &other)
  {
    input_size = other.input_size;

    //direct copy of reference as indicator doesn't own this data
    //Note a shared pointer (such as boost::shared_ptr) would be better than a naked reference
    input_array = other.input_array; 
    //destroy old data and construct a new set of data
    delete[] value_array;
    value_array = new double[input_size]; 
    //do you want to copy the data too? maybe a memcpy follows?
    memcpy(value_array, other.value_array, input_size*sizeof(double));
  }
  return *this;
}

您可能还希望将析构函数设置为虚拟 - 请参阅here了解原因 - 它有助于防止SMA析构函数中的内存泄漏

virtual ~Indicator() { delete[] value_array; }

答案 2 :(得分:0)

使用 std::vector 代替原始数组。

std::vector处理所有内存管理和复制等等。

干杯&amp;第h。,