奎斯顿与'const' [英] Quesiton with 'const'

查看:87
本文介绍了奎斯顿与'const'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果我在班上定义一个const stl vector属性,


class A {

public:

A();

private:

const vector< int> v;

};


我还可以在A'的构造函数外面的列表中添加/删除元素吗?


谢谢。

解决方案



ying ... @ gmail.com写道:< blockquote class =post_quotes>

如果我在班上定义一个const stl vector属性,

A类{
public:
A();
私人:
const vector< int> v;
};

我还可以在A'的构造函数外面的列表中添加/删除元素吗?




[最好不要将矢量描述为列表,因为列表是不同的

标准容器]


编号也不能添加或删除元素_inside_ A的构造函数。

你可以在A'的构造函数'

初始化列表中选择构造向量,但是你不能更改。这就是const

的意思。在这种情况下你认为它意味着什么?


Gavin Deane


yi ***** @ gmail.com 写道:

如果我在我的班级中定义const stl vector属性,

A类{
公开:
A();
私人:
const vector< int> v;
};

我还可以在A'的构造函数外面的列表中添加/删除元素吗?




否如果你在施工结束后可以更改

,那么宣布它是'const'会有什么意义呢?


V

-

请在邮寄回复时从我的地址中删除资金




Victor Bazarov写道:< blockquote class =post_quotes> yi*****@gmail.com 写道:

如果我在我的班级定义一个const stl vector属性,

A类{
public:
A();
private:
const vector< INT> v;
};

我还可以在A'的构造函数外面的列表中添加/删除元素吗?



不会是什么如果你可以在施工后改变它,那么宣称它是常量的点?

V




目标是可能要做出v在A'的上下文中是只读的,但在某些其他上下文中可以读/写



声明成员向量const可以防止任何人修改它。

但是指向const对象的指针不需要指向声明为

const的对象。所以形式的成员声明:


const vector< int> * v;


可以指向非const std:vector< ; INT取代;所有访问矢量

到v虽然必须将矢量视为常量。


格雷格


Hi,

If I define a const stl vector attribute in my class,

class A {
public:
A();
private:
const vector<int> v;
};

can I still add/remove elements to the list outside A''s constructor?

Thank you.

解决方案


ying...@gmail.com wrote:

Hi,

If I define a const stl vector attribute in my class,

class A {
public:
A();
private:
const vector<int> v;
};

can I still add/remove elements to the list outside A''s constructor?



[Best not to describe a vector as a list, since a list is a different
standard container]

No. Neither can you add or remove elements _inside_ A''s constructor.
You can construct the vector is any way you choose in A''s constructor''s
initialisation list, but then you can''t change it. That''s what const
means. What did you think it meant in this context?

Gavin Deane


yi*****@gmail.com wrote:

If I define a const stl vector attribute in my class,

class A {
public:
A();
private:
const vector<int> v;
};

can I still add/remove elements to the list outside A''s constructor?



No. What would be the point of declaring it ''const'' if you could change
it after construction?

V
--
Please remove capital As from my address when replying by mail



Victor Bazarov wrote:

yi*****@gmail.com wrote:

If I define a const stl vector attribute in my class,

class A {
public:
A();
private:
const vector<int> v;
};

can I still add/remove elements to the list outside A''s constructor?



No. What would be the point of declaring it ''const'' if you could change
it after construction?

V



The aim is probably to make "v" read-only in A''s context, but
read/writeable in some other context.

Declaring the member vector const prevents anyone from modifying it.
However a pointer to a const object need not point to a object declared
const. So a member declaration of the form:

const vector<int>* v;

could point to a non-const std:vector<int>; all access to the vector
through v though would have to treat the vector as const.

Greg


这篇关于奎斯顿与'const'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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