简体   繁体   English

如何将参考向量作为类成员?

[英]How to have reference vector as a class member?

I have a class that holds a vector.我有一个包含向量的类。 I want it so that if the original vector is modified, then the class member vector will also be modified.我想要这样,如果原始向量被修改,那么类成员向量也将被修改。 Ie the class member vector is a reference to the original vector:即类成员向量是对原始向量的引用:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Entity {
    public:
    int x;
    Entity() {}
    Entity(int y) : x(y) {}
};

class Me {
    public:
    vector<Entity> vec;
};

Me me;

int main()
{
  Entity e1(1);
  Entity e2(2);
  vector<Entity> vec;
  vec.push_back(e1);
  vec.push_back(e2);
  me.vec = vec;
  
  me.vec[0].x = 5;
  
  cout << vec[0].x << endl;
  return 0;
}

This outputs 1 when I want it to output 5.这种输出1时,我希望它输出5。

In your snippet, the two vector s are independent instances (apart from one being initialized by the other).在您的代码片段中,两个vector是独立的实例(除了一个被另一个初始化)。 With this,有了这个,

class Me {
    public:
    vector<Entity> vec;
};

/* ... */

me.vec = vec;

you copy the content of vec into me.vec and any modification of either me.vec or vec don't affect the other.您将vec的内容复制到me.vec并且对me.vecvec任何修改都不会影响另一个。 What you want is this:你想要的是这个:

class Me {
    public:
    vector<Entity> *vec; // now a pointer to a vector
};

/* ... */

me.vec = &vec; // initialize the pointer with the address of vec

Now, any changes to vec will be reflected by an access through me.vec .现在,对vec任何更改都将通过me.vec访问反映出来。 Note that this ships with the necessity to take care of the pointer - it can be nullptr , in which case you must not dereference it.请注意,这需要处理指针 - 它可以是nullptr ,在这种情况下,您不能取消引用它。 And it can change the location it points to, which can be error prone.它可以改变它指向的位置,这很容易出错。 As an alternative, consider storing a reference to a vector in Me .作为替代方案,考虑在Me存储对向量的引用。 A reference has no null-state and can only be bound once.引用没有空状态,只能绑定一次。 On the flip side, you have to initialize Me instances with an existing vector instance.另一方面,您必须使用现有向量实例初始化Me实例。

You have two options here...你在这里有两个选择......

Use a vector reference使用矢量参考

Have the class hold a vector<Entity> & , or a vector reference .让班级持有vector<Entity> &或 vector reference This will require you to pass the vector you're referencing to the class at the time of creation.这将要求您在创建时将引用的向量传递给类。 That could look like:这可能看起来像:

class Me {
    public:
    vector<Entity> &vec;
    Me(vector<Entity> &v): vec(v) { }
};

// ...

int main() {
    vector<Entity> vec;
    Me me(vec);
    // ...
    
    me.vec[0].x = 5;

    cout << vec[0].x << endl;
}

Use pointers使用指针

If it didn't occur to you to use pointers, then I'd say that you should probably avoid them here, as you likely don't have the understanding to implement them safely.如果您没有想到使用指针,那么我会说您应该在这里避免使用它们,因为您可能不了解安全地实现它们。

That being said, if you're interested, I'd look into Smart Pointers .话虽如此,如果您有兴趣,我会研究Smart Pointers

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

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