简体   繁体   English

指针向量

[英]A vector of pointers

I am trying to read data in from a file, create a critic from the data and then create a vector of pointers to each critic. 我试图从文件中读取数据,从数据中创建评论者,然后创建指向每个评论者的指针向量。 After I read the data for the first critic and set it to theCritic, I have 在我阅读了第一个评论家的数据并将其设置为“批评”之后,

NightHawks *nightHawkPtr = &theCritic; 
criticPointer.push_back(nightHawkPtr);

and then I try to print them out. 然后我尝试将它们打印出来。

 for (int i = 0; i < criticPointer.size(); i++)
    {
        criticPointer[i]->text();
    }

However, each time the pointer is pushed back, all the pointers point to the same critic. 但是,每次将指针向后推时,所有指针都指向同一评论家。

From your code, it looks like you are setting all the pointer values to one instance and just keep changing that instance. 从您的代码看来,您正在将所有指针值设置为一个实例,而只是不断更改该实例。 You must create new instances everytime the value changes: 每当值更改时,您都必须创建新实例:

NightHawks *nhp = new Critic();

If you are inserting them into a vector, it is strongly advised to use shared_ptrs so you don't have to worry about freeing memory. 如果要将它们插入向量中,强烈建议使用shared_ptrs,这样就不必担心释放内存。

They point to the same critic because you assign them the address of the same variable theCritic 它们指向同一评论者,因为您为他们分配了相同变量theCritic的地址

NightHawks *nightHawkPtr = &theCritic; 
criticPointer.push_back(nightHawkPtr);

Moreover if variable theCritic is a local variable then the program can have undefined behaviour because a pointer to the variable becames invalid after the local variable will be destroyed. 此外,如果变量theCritic是局部变量,则程序可能具有未定义的行为,因为指向变量的指针在局部变量将被销毁后变为无效。

Each time you build an object of type critic you should build it in the heap that is you should use operator new . 每次构建类型为注释器的对象时,都应在堆中构建它,即应使用运算符new

You need to make a new critic when you place it in the vector. 将其放置在向量中时,需要创建一个新的注释器。 Else all the data point to the same memory location. 否则所有数据都指向相同的存储位置。

criticPointer.push_back(new NightHawks(theCritic));

Better Yet don't store pointers just store the critic value in the vector. 更好的是,不存储指针,而只是将评论者值存储在向量中。

As you mentioned in your comment that, I declare the critic like this NightHawks theCritic; and then i use getLine to read the input from the file and then I use setters to set the input to theCritic 正如您在评论中提到的那样, I declare the critic like this NightHawks theCritic; and then i use getLine to read the input from the file and then I use setters to set the input to theCritic I declare the critic like this NightHawks theCritic; and then i use getLine to read the input from the file and then I use setters to set the input to theCritic , your problem is that you are changing the contents of the same variable. I declare the critic like this NightHawks theCritic; and then i use getLine to read the input from the file and then I use setters to set the input to theCritic ,您的问题是您正在更改同一变量的内容。 Create new variables for different critics, assign to them, then push the address into the vector. 为不同的评论者创建新变量,分配给他们,然后将地址放入向量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM