向对象添加对象会覆盖现有对象

时间:2015-12-21 18:25:22

标签: c++

我有两个类: GenomeRankedNode RandomRankedTPNGenerator RandomRankedTPNGenerator 职责只是随机创建 GenomeRankedNode GenomeRankedNode 有四个属性:int,int,int *和int。我的主要有向量,应使用 RandomRankedTPNGenerator 进行填充。为此,我主要有:

std::vector<GenomeRankedNode*> population;
population.resize(50);
for (int i = 0; i < popsize; i++ ) {
    RandomRankedTPNGenerator* generator = new RandomRankedTPNGenerator();
    GenomeRankedNode node* = generator->randomNode(numParents);
    population[i] = node;
    delete generator;
}

RandomRankedTPNGenerator 中,重要的函数是randomNode():

GenomeRankedNode* RandomRankedTPNGenerator::randomNode(int numParents){
    int function = randomFunction(); //just gets a random number
    int* weights = randomWeights(numParents); //just gets a random number
    int variance = randomVariance(); //just gets a random number
    GenomeRankedNode* node = new GenomeRankedNode(function, numParents, weights, variance); 
    return node;
}

以上代码正确生成随机 GenomeRankedNode 。问题是,在人口中,当我添加对象时,现有的对象显然被“覆盖”(我知道它们实际上并没有被覆盖......)。另一方面,人口正在填充不同的地址。如果我使用 RandomRankedTPNGenerator 的不同实例来创建 GenomeRankedNode ,则在不同地址中创建 GenomeRankedNode 对象,为什么所有指针都在人口指向内存中的相同空间,导致每个i的population [i]指向的值是相同的?

以下是 GenomeRankedNode.cpp 的代码:

int function;
int* weights;
int numNodeParents;
int variance;

double vars[11] = {0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10, 50};

GenomeRankedNode::GenomeRankedNode()
{
    function = 0;
    weights = 0;
    numNodeParents = 1;
    variance = 0;
}

GenomeRankedNode::GenomeRankedNode(int inFunction, int inNumParents, int* inWeights, int inVariance)
{
    function = inFunction;
    numNodeParents = inNumParents;
    weights = inWeights;
    variance = inVariance;
}

GenomeRankedNode::GenomeRankedNode(GenomeRankedNode* inNode)
{
    function = inNode->getFunction();
    numNodeParents = inNode->getNumParents();
    weights = inNode->getWeights();
    variance = inNode->getVariance();
}

GenomeRankedNode::GenomeRankedNode(const GenomeRankedNode &inNode)
{
    function = inNode.getFunction();
    numNodeParents = inNode.getNumParents();
    variance = inNode.getVariance();

    //deep copy
    if (inNode.getWeights()){
        weights = new int[numNodeParents];
        for (int i = 0; i < numNodeParents; i++)
            weights[i] = inNode.getWeights()[i];
    }
}

GenomeRankedNode& GenomeRankedNode::operator= (const GenomeRankedNode &inNode)
{

    //self-assignment check
    if (this == &inNode)
        return *this;

    function = inNode.getFunction();
    numNodeParents = inNode.getNumParents();
    variance = inNode.getVariance();

    // explicitly deallocate values
    delete[] weights;

    // deep copy
    if (inNode.getWeights()){
        weights = new int[numNodeParents];
        for (int i = 0; i < numNodeParents; i++)
            weights[i] = inNode.getWeights()[i];
    }
    else
        weights = 0;

    return *this;
}

GenomeRankedNode::~GenomeRankedNode(void)
{
    delete &function;
    delete &numNodeParents;
    delete weights;
    delete &variance;
}

int GenomeRankedNode::getFunction() const
{
    return function;
}

int* GenomeRankedNode::getWeights() const
{
    return weights;
}

int GenomeRankedNode::getNumParents() const
{
    return numNodeParents;
}

int GenomeRankedNode::getVariance() const
{
    return variance;
}

void GenomeRankedNode::setWeights(int *inWeights)
{
    weights = inWeights;
}

void GenomeRankedNode::setFunction(int inFunction)
{
    function = inFunction;
}

void GenomeRankedNode::setVariance(int inVariance)
{
    variance = inVariance;
}

double GenomeRankedNode::getRealVariance(int inVariance) const
{
    return vars[inVariance];
}

为了检查不当行为,我加入了main:

for (int i = 0; i < population.size(); i++){
  cout << population.at(i)->toString();

}

鉴于population.size()= 3,输出为:

Function: 1
Weights: 1 1
Variance: 2

Function: 1
Weights: 1 1
Variance: 2

Function: 1
Weights: 1 1
Variance: 2

另一方面,如果我添加以下行:

cout << population.at(i)->toString(); 

在我用来生成随机GenomeRankedNode对象的循环中,我有:

Function: 0
Weights: 3 4
Variance: 1

Function: 1
Weights: 4 1
Variance: 3

Function = 1
Weights = 1 1
Variance = 2

鉴于此,我的结论是:由于某种原因,所有创建的GenomeRankedNode都指向内存中的同一个插槽。因此,每当实例化新的GenomeRankedNode时,所有其他指针(GenomeRankedNode *)都将更新其指向值!它类似于浅层和深层复制问题,但我不知道它来自何处!

2 个答案:

答案 0 :(得分:2)

我看到的一个问题是你将人口的第i个值设置为GenomeRankedType而不是GenomeRankedType*(你说人口存储的类型)。

所以将代码的顶部修改为:

std::vector<GenomeRankedNode*> population;
population.resize(50);
for (int i = 0; i < popsize; i++ ) {
    RandomRankedTPNGenerator* generator = new RandomRankedTPNGenerator();
    GenomeRankedNode* node = generator->randomNode(numParents);
    population[i] = node;
    delete generator;
}

请告诉我这是否对您有帮助!

答案 1 :(得分:0)

我感动:

int function;
int* weights;
int numNodeParents;
int variance;

从GenomeRankedNode.cpp到GenomeRankedNode.h为私人。这解决了这个问题。