简体   繁体   English

矢量的指针

[英]vector of pointers

If I have a definition in a class header such as this: 如果我在类标题中有一个定义,例如:

vector<baddie*> baddies;

which I then initialise in the constructor like this: 然后我在构造函数中初始化,如下所示:

Class::Class(vector<baddie*> input)
{
    baddies = input;
}

What do I end up with? 我最终得到了什么? Two vectors with two sets of pointers pointing to the objects? 有两组指针指向对象的两个向量?

Would it be better to simply point to the original vector? 简单地指向原始矢量会更好吗? Is this even possible? 这甚至可能吗?

Or would it be better to hold a vector of pointer references in the class so as to not duplicate pointers? 或者更好的是在类中保存指针引用的向量,以便不重复指针? What is the best practice for accessing objects and arrays of objects in multiple classes? 访问多个类中的对象和对象数组的最佳实践是什么? References? 参考文献? Pointers? 指针? Pointer references? 指针引用? Thank you in advance! 先感谢您!

It depends on the semantics that you want to provide. 这取决于您要提供的语义。 In C++11 what you would probably want to do is something like: 在C ++ 11中,您可能想要做的是:

Class::Class( vector<baddie*> input ) : baddies( std::move(input) ) {}

Which will move the memory from the copy in the argument to the member, while in C++03 you would probably write: 这会将内存从参数中的副本 移动到成员,而在C ++ 03中你可能会写:

Class::Class( vector<baddie*> const & input ) : baddies( input ) {}

Which will initialize the vector with a copy. 这将使用副本初始化向量。

Note that this discussion only relates to the vector contents, not to the data pointed by those baddie pointers. 请注意,此讨论仅涉及向量内容,而不涉及那些baddie指针所指向的数据。 That is, in both cases, there will be two vectors with pointers to refer to the same elements (ie only one copy of each baddie in memory, with two pointers referring to it). 也就是说,在两种情况下,都会有两个带有指针的向量来引用相同的元素(即内存中每个baddie只有一个副本,两个指针指向它)。

Depending on the semantics of your application, you might want to go from this intermediate shallow copying to either end: Perform a deep copy (ie create new baddie elements in memory, so that the original and the copy are fully unrelated once the constructor completes) or avoid coping at all, and just store a reference/pointer so that both vectors are exactly the same (insertion inside or outside of the class will be visible outside/inside the class). 根据应用程序的语义,您可能希望从这个中间浅层复制到任一端:执行深层复制 (即在内存中创建新的baddie元素,以便在构造函数完成后原始和副本完全无关)或避免应付在所有,只存储一个参考/指针使得两个矢量是完全一样的 (内部或外部的类的插入将是可见的外部/里面的类)。

Also, beware of vector of pointers, you need to manage the memory manually before the vector is destructed. 另外,要注意指针向量,需要在向量被破坏之前手动管理内存。

Well if you want to share the data I would probably use a std::vector< std::shared_ptr<baddie> > or a boost::shared_ptr< boost::ptr_vector<baddie> > 好吧,如果你想共享数据,我可能会使用std::vector< std::shared_ptr<baddie> >或者a boost::shared_ptr< boost::ptr_vector<baddie> >

However it actually depends on: 但它实际上取决于:

  • your intentions 你的意图
  • if it is necessary to have shared data 如果有必要共享数据
  • if "baddie" is really that expensive to construct/copy 如果“baddie”构建/复制真的那么昂贵

besides: you should use the initializer list to initialize your members: 此外:您应该使用初始化列表来初始化您的成员:

Class:Class(vector<baddie> input)
: baddies( input ) 
{}

That is a bad way of doing it because first it copies the vector to the constructor and then it copies it to the vector called baddies, in this case its better to have the input as a reference and then copy it to baddies. 这是一种不好的方法,因为首先它将向量复制到构造函数然后将其复制到名为baddies的向量,在这种情况下,最好将输入作为引用然后将其复制到baddies。 If you want to have control of the copy then you could consider traversing the input vector and pushing each value to the baddies vector. 如果你想控制副本,那么你可以考虑遍历输入向量并将每个值推送到baddies向量。 If you want to get more speed then you could declare the baddies vector and input vector both as pointers and then just point the baddies to where the input vector comes from. 如果你想获得更快的速度,那么你可以将baddies向量和输入向量都声明为指针,然后将坏人指向输入向量来自的位置。 This how ever makes it a bit ugly to use because if you index the vector with [] then you will first be indexing the pointer so you would have to index it twice to reach the vector, like this: 这有点让它使用起来有点难看,因为如果用[]索引向量,那么你将首先索引指针,所以你必须将它索引两次才能到达向量,如下所示:

baddies[0][i];

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

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