Why do the objects created in a loop have the same address?

前端 未结 3 1454
感情败类
感情败类 2020-12-11 04:22

I do see some other questions that look like mine, but I still can\'t figure this out.

Here\'s my code:

#include
#include 

        
3条回答
  •  情话喂你
    2020-12-11 05:02

    Here;

    node newNode = node(i,NULL);
    

    You are not creating a new node, just assigning a new value to it.

    New nodes are being created inside the vector using the default copy constructor. If you want your vector to contain the nodes you are pushing into it you should use a pointer, like this;

    vector*> vectorOfNodes;
    

提交回复
热议问题