const正确性 - C ++更喜欢const成员而非const吗? [英] const correctness - should C++ prefer const member over non-const?

查看:59
本文介绍了const正确性 - C ++更喜欢const成员而非const吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了这个类:

class foo {

std :: vector< int> data;

public:


int operator [](int n){

返回数据[n];

}

int运算符[](int n)const {

返回数据[n];

}

};

现在在我的程序中我做:


foo myFoo;

int x = myFoo [123];

....

是否应调用foo :: operator []的const版本?


我认为应该这样,但我的编译器不同意我的意思。


什么是正确的行为?为什么......?


-

< \ ___ />

/ OO \

\ _____ / FTB。

http:// www.topaz3d.com/ - 新的3D编辑器!

I define this class:
class foo {
std::vector<int>data;
public:

int operator[](int n) {
return data[n];
}
int operator[](int n) const {
return data[n];
}
};
Now in my program I do:

foo myFoo;
int x = myFoo[123];
....
Should the const version of foo::operator[] be called?

I think it should, but my compiler disagrees with me.

What''s the correct behavior? Why...?

--
<\___/>
/ O O \
\_____/ FTB.

http://www.topaz3d.com/ - New 3D editor!

推荐答案

文章

< 79 * ******** @ m74g2000hsh。 googlegroups.com>,

fungus< op *********** @ artlum.comwrote:
In article
<79**********************************@m74g2000hsh. googlegroups.com>,
fungus <op***********@artlum.comwrote:

I定义这个类:


class foo {

std :: vector< int> data;

public:


int operator [](int n){

返回数据[n];

}

int运算符[](int n)const {

返回数据[n];

}

};


现在在我的程序中我做:


foo myFoo;

int x = myFoo [123];

。 ..


是否应调用foo :: operator []的const版本?


我认为应该这样,但我的编译器不同意我。


什么是正确的行为?为什么...?
I define this class:
class foo {
std::vector<int>data;
public:

int operator[](int n) {
return data[n];
}
int operator[](int n) const {
return data[n];
}
};
Now in my program I do:

foo myFoo;
int x = myFoo[123];
...
Should the const version of foo::operator[] be called?

I think it should, but my compiler disagrees with me.

What''s the correct behavior? Why...?



我对你为什么认为它应该调用const版本感兴趣。如果它b / b
,它何时会调用非const版本?

I''m interested in why you think it should call the const version. If it
did, when would it ever call the non-const version?


在文章< 7908dabe-5102-4669-bf68- 6cd36bedd6a7

@ m74g2000hsh.googlegroups.com>, op ******* ****@artlum.com 说...
In article <7908dabe-5102-4669-bf68-6cd36bedd6a7
@m74g2000hsh.googlegroups.com>, op***********@artlum.com says...

我定义这个类:


class foo {

std :: vector< int> data;

public:


int operator [](int n){

返回数据[n];

}

int operator [](int n)const {

return data [n];

}

};


现在在我的程序中我做:

foo myFoo;

int x = myFoo [123];

...


应该是const调用foo :: operator []的版本?
I define this class:
class foo {
std::vector<int>data;
public:

int operator[](int n) {
return data[n];
}
int operator[](int n) const {
return data[n];
}
};
Now in my program I do:

foo myFoo;
int x = myFoo[123];
...
Should the const version of foo::operator[] be called?



No.

No.


我认为应该这样,但我的编译器不同意我的观点。
I think it should, but my compiler disagrees with me.



编译器是正确的。应该为const

对象调用const版本。应该为非const对象调用非const版本。

如果你没有/没有非const版本,那么const版本
$可以调用b $ b - 但是首先将非const转换为

a const对象。这很好(在这种情况下),但它仍然是一个

转换。当匹配重载函数时,不需要转换为
a的函数比需要转换的函数更好。


大多数人都想要const在作业的RHS上使用的版本,但是对于LHS来说,非常数是
。为了得到那种行为,你通常会使用一个代理对象来重载operator =和operator T.


-

后来,

杰瑞。


宇宙是自己想象的虚构。

The compiler''s right. The const version should be called for a const
object. The non-const version should be called for a non-const object.
If you don''t/didn''t have a non-const version, then the const version
could be called -- but it would be called by convertion the non-const to
a const object first. That''s fine (in this case) but it''s still a
conversion. When matching overloaded functions, one that doesn''t require
a conversion is a better match than one that does require a conversion.

Most people want the const version used on the RHS of an assignment, but
the non-const for the LHS. To get what that kind of behavior, you
normally use a proxy object that overloads operator= and operator T.

--
Later,
Jerry.

The universe is a figment of its own imagination.


10月30日,6:31 * am,blargg .... @ gishpuppy.com(blargg)写道:
On Oct 30, 6:31*am, blargg....@gishpuppy.com (blargg) wrote:

>

我对你为什么认为应该调用const版本感兴趣。如果它b / b
,它何时会调用非const版本?
>
I''m interested in why you think it should call the const version. If it
did, when would it ever call the non-const version?



好​​吧,也许我过度简化了它。假设运算符[]

返回对int的引用:


int& operator [](int n){

返回数据[n];

}

int& operator [](int n)const {

返回数据[n];

}


如果表达式在任务的RHS,应该调用const版本吗?

-

< \ ___ />

/ OO \

\ _____ / FTB。

http://www.topaz3d.com/ - 新的3D编辑器!

Ok, maybe I oversimplified it. Supposed operator[]
returns a reference to the int:

int& operator[](int n) {
return data[n];
}
int& operator[](int n) const {
return data[n];
}

If the expression is on the RHS of an assignment, should
the const version be called?
--
<\___/>
/ O O \
\_____/ FTB.

http://www.topaz3d.com/ - New 3D editor!


这篇关于const正确性 - C ++更喜欢const成员而非const吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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