我有两个类: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由人口[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();


}

给定人口。大小()= 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 *)都将更新其指向的值!它类似于浅层复制与深层复制问题,但我不知道它的起源!

最佳答案

我看到的一个问题是,您将总体的ith值设置为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;
}


让我知道这是否对您有帮助!

关于c++ - 向 vector 添加对象会覆盖现有对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34402062/

10-11 01:04