为什么我可以修改const std :: vector& ??? [英] Why Can I Modify A const std::vector& ???

查看:59
本文介绍了为什么我可以修改const std :: vector& ???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对const引用感到困惑。我认为他们提供了一种方式来获得私人会员的访问权限,而不允许这些会员被改变为
。但是,以下客户端代码成功删除了引用成员向量的



PointCollection pc;

pc.addSomePoints() ;

std :: vector< Point> mem = pc.getPoints(); //返回一个const ref

cout<< mem.size()<< ENDL; //输出3,或者其他什么#

点恰好是

mem.clear();

cout<< mem.size()<< ENDL; //输出0 !!


以下是相关课程:


class PointCollection {

private :

std :: vector< int> m_points;

public:

const std :: vector< Point>& PointCollection :: getPoints()

{

返回m_points;

}

//其他添加点的方法等等,遗漏了

简洁

}


感谢您的帮助,

cpp

I''m confused about const references. I thought they provided a way to
give access to private members without allowing those members to be
changed. However, the following client code successfully deletes the
referred to member vector:

PointCollection pc;
pc.addSomePoints();
std::vector<Point> mem = pc.getPoints(); //returns a const ref
cout << mem.size() << endl; //outputs 3, or whatever # of
points happens to be
mem.clear();
cout << mem.size() << endl; //outputs 0!!

Here is the the class in question:

class PointCollection {
private:
std::vector<int> m_points;
public:
const std::vector<Point>& PointCollection::getPoints()
{
return m_points;
}
//other methods to add points, etc, left out for
brevity
}

Thanks for any help,
cpp

推荐答案

cppaddict写道:
cppaddict wrote:
我对const引用感到困惑。我认为他们提供了一种方法来访问私人会员而不允许这些会员进行更改。但是,以下客户端代码成功删除了
引用的成员向量:

PointCollection pc;
pc.addSomePoints();
std :: vector< Point> mem = pc.getPoints(); //返回一个const ref
cout<< mem.size()<< ENDL; //输出3,或者其他任何
点恰好是
mem.clear();
cout<< mem.size()<< ENDL; //输出0 !!

以下是相关课程:

类PointCollection {
私有:
标准符号::向量< int> m_points;
public:
const std :: vector< Point>& PointCollection :: getPoints()
{
返回m_points;
}
//添加点等的其他方法,遗漏了
简洁
}

感谢您的帮助,
cpp
I''m confused about const references. I thought they provided a way to
give access to private members without allowing those members to be
changed. However, the following client code successfully deletes the
referred to member vector:

PointCollection pc;
pc.addSomePoints();
std::vector<Point> mem = pc.getPoints(); //returns a const ref
cout << mem.size() << endl; //outputs 3, or whatever # of
points happens to be
mem.clear();
cout << mem.size() << endl; //outputs 0!!

Here is the the class in question:

class PointCollection {
private:
std::vector<int> m_points;
public:
const std::vector<Point>& PointCollection::getPoints()
{
return m_points;
}
//other methods to add points, etc, left out for
brevity
}

Thanks for any help,
cpp




以下行没有按照您的想法行事:

std :: vector< Point> mem = pc.getPoints(); //返回一个常量引用


你的getPoints成员可能确实返回一个常量引用,

但是它被用于初始化Points的向量。也就是说,你是
制作任何getPoints的副本(通过vector'的复制构造函数)

返回对该副本的引用,然后对该副本进行操作。如果您只是想要一个可以在本地使用的参考,请尝试:

const std :: vector< Point> & mem = pc.getPoints();


Alan



The following line is not doing what you think it is doing:
std::vector<Point> mem = pc.getPoints(); //returns a const ref

Your "getPoints" member may indeed be returning a constant reference,
but it is being used to initialize a vector of Points. That is, you are
making a copy (via vector''s copy constructor) of whatever getPoints
returns a reference to, and then operating on that copy. If you just
want a reference you can use locally, try:
const std::vector<Point> &mem = pc.getPoints();

Alan





cppaddict写道:


cppaddict wrote:
我对const引用感到困惑。我认为他们提供了一种方法来访问私人会员而不允许这些会员进行更改。但是,以下客户端代码成功删除了
引用的成员向量:

PointCollection pc;
pc.addSomePoints();
std :: vector< Point> mem = pc.getPoints(); //返回一个const ref
I''m confused about const references. I thought they provided a way to
give access to private members without allowing those members to be
changed. However, the following client code successfully deletes the
referred to member vector:

PointCollection pc;
pc.addSomePoints();
std::vector<Point> mem = pc.getPoints(); //returns a const ref




在这里你要调用''mem''上的拷贝构造函数。现在,''mem''是''pc.getPoints()''的
*副本*。它本身不是const引用。那个

看起来像这样:

const std :: vector< Point>& mem = pc.getPoints();


/ david



Here you are invoking the copy constructor on ''mem''. Now, ''mem'' is a
*copy* of ''pc.getPoints()''. It is not itself a const reference. That
would look like this:

const std::vector<Point>& mem = pc.getPoints();

/david


你的" getPoints"成员可能确实返回一个常量引用,
但它被用来初始化一个点向量。也就是说,你正在制作一个副本(通过vector'的复制构造函数)来获取任何getPoints
返回引用,然后对该副本进行操作。如果您只是想要一个可以在本地使用的参考,请尝试:
const std :: vector< Point> & mem = pc.getPoints();
Your "getPoints" member may indeed be returning a constant reference,
but it is being used to initialize a vector of Points. That is, you are
making a copy (via vector''s copy constructor) of whatever getPoints
returns a reference to, and then operating on that copy. If you just
want a reference you can use locally, try:
const std::vector<Point> &mem = pc.getPoints();




艾伦,


非常感谢你。这正是我想要的。


cpp



Alan,

Thank you very much. That is exactly what I want.

cpp


这篇关于为什么我可以修改const std :: vector&amp; ???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆