C++ 返回对对象向量的常量引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1572158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning a const reference to vector of an object
提问by éric Malenfant
I have 2 questions related to the same problem:
我有两个与同一问题相关的问题:
How can I return a reference to a vector which belongs to a class?
I have this class:
class sys{ protected: vector<int> s; public: sys(); vector<int>& getS() {return s;} //(1) };
(1) should return the reference of the vector
s
. However, inmain()
:main(){ sys* my_sys = new sys(); vector<int> &t1 = my_sys->getS(); //(2) vector<int> t2 = my_sys->getS(); //(3) ... }
t1
is a reference tos
(i.e. whent1
is changedmy_sys.s
is changed as well).t2
is a COPY ofs
(i.e. whent2
is changed my_sys.s is NOT changed).
Why does line (3) work?
I do not want it to be possible to change
my_sys.s
outside of the class, but I want to return a reference because of efficiency. Where do I put theconst
?I tried to change line (1) to
const vector<int>& getS() {return s;} //(4)
but I am not sure if that is enough.
如何返回对属于类的向量的引用?
我有这门课:
class sys{ protected: vector<int> s; public: sys(); vector<int>& getS() {return s;} //(1) };
(1) 应该返回向量的引用
s
。然而,在main()
:main(){ sys* my_sys = new sys(); vector<int> &t1 = my_sys->getS(); //(2) vector<int> t2 = my_sys->getS(); //(3) ... }
t1
是对s
(即何时t1
更改my_sys.s
也更改)的引用。t2
是一个副本s
(即何时t2
更改 my_sys.s 未更改)。
为什么第 (3) 行有效?
我不希望它可以在
my_sys.s
课外进行更改,但是由于效率的原因,我想返回一个引用。我把const
?我试图将第 (1) 行更改为
const vector<int>& getS() {return s;} //(4)
但我不确定这是否足够。
回答by éric Malenfant
Line 3 works because t2 is copy-constructed from the reference returned by getS()
第 3 行有效,因为 t2 是从 getS() 返回的引用复制构造的
The way you const-qualify the reference returned by getS() is OK. You could const-qualify getS() as well, i.e.:
您对 getS() 返回的引用进行常量限定的方式是可以的。您也可以对 getS() 进行常量限定,即:
const vector<int>& getS()const;
so that getS() could be called on a const sys.
这样 getS() 就可以在 const sys 上调用。
回答by morechilli
Line 3 works because c++ calls the copy constructor on the vector.
第 3 行有效,因为 c++ 调用向量上的复制构造函数。
Your function returns a reference, that reference is passed to the vector copy constructor and your variable t2 is constructed.
您的函数返回一个引用,该引用被传递给向量复制构造函数并构造您的变量 t2。
This is allowed as the copy constructor of vector is not defined as explicit.
这是允许的,因为 vector 的复制构造函数未定义为显式。
You cannot guard against this with a general type. In your own class you can mark the copy constructor explicit and the assignment would fail.
你不能用一般类型来防止这种情况。在您自己的类中,您可以将复制构造函数标记为显式,并且赋值将失败。
You could return a const pointer instead. This would guard against the copy - but might be dangerous as users may expect to be able to pass the pointer beyond its valid scope.
您可以改为返回一个 const 指针。这将防止复制 - 但可能是危险的,因为用户可能希望能够将指针传递到其有效范围之外。
const vector<int>* getS() {return &s;} //(4)
回答by foraidt
Line (3)
线 (3)
For the int type it is trivial to copy. If you put objects in there and they have a Copy CTor, this will also work.
对于 int 类型,复制是微不足道的。如果您将对象放在那里并且它们有一个 Copy CTor,这也将起作用。
Line (4)
线 (4)
Make it
做了
const vector<int>& getS() const {return s;}
so the function is also declared as const.
And call it like this
所以函数也被声明为const。
并这样称呼它
const vector<int> & t1 = my_sys->getS();