由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - question regarding const function
相关主题
再问一下[合集] 请问-fno-implicit-templates的用处
which func will be called?请叫一个 template class constructor 的问题
问题c++ question
c++ operator overloading questionconst 指针类型转换
An object of A automatically converted to an object of B.请教各路C++大神 为什么f(3) 输出是 'dd'
Re: VC里面的stl支持是不是很弱?问两个C++语法问题
a string define question (c++)请问一个implicit conversion的问题(C++)
C++ implicit typename的问题one question about initializaiton list
相关话题的讨论汇总
话题: const话题: xyz话题: person话题: name话题: aperson
进入Programming版参与讨论
1 (共1页)
c*******9
发帖数: 6411
1
class Person
{
public:
string name() const;
...
};
Person aperson;
theName = aperson.name();
Question, since name is a const function, does that mean it can only be
called by a const object? the above code is ok, so does that mean that
the object aperson will be implicited converted to "const object" before
calling name()...? otherwise how come it is allowed to call name()...
Thanks.
t****t
发帖数: 6806
2
yes, the object will be implicitly "converted" to const.

【在 c*******9 的大作中提到】
: class Person
: {
: public:
: string name() const;
: ...
: };
: Person aperson;
: theName = aperson.name();
: Question, since name is a const function, does that mean it can only be
: called by a const object? the above code is ok, so does that mean that

z***9
发帖数: 696
3
my understanding is that function name() const should not modify any data
member in the class, however, it does not mean an object of this class has
to be const in order to call name().
t****t
发帖数: 6806
4
since non-const -> const conversion is almost always implicitly there, you
can say the object will be const when you call it. but of course, it doens't
have to be already const.

【在 z***9 的大作中提到】
: my understanding is that function name() const should not modify any data
: member in the class, however, it does not mean an object of this class has
: to be const in order to call name().

d****p
发帖数: 685
5
name =
void name(const Person* person)
So when you call name() via aperson.name(), what happened is
name(&aperson) where &aperson is converted to const Person*
The conversion is OK since it is from less cv-qualified to more cv-qualified
. The other way around is not allowed unless you use const_cast

【在 z***9 的大作中提到】
: my understanding is that function name() const should not modify any data
: member in the class, however, it does not mean an object of this class has
: to be const in order to call name().

c*******9
发帖数: 6411
6
void name(const Person* person)
So when you call name() via aperson.name(), what happened is
name(&aperson)
could you explain where the following come from? it is not in the signature
, it the following generated by the compiler?
void name(const Person* person)
thanks...
z****e
发帖数: 2024
7
我们来验证一下这个转换的存在性。
class xyz
{
public:
const xyz& operator+(const xyz &rhs) const{return *this;}
private:
int n;
};
如果你把
const xyz& operator+(const xyz &rhs) const{return *this;}
变成
xyz& operator+(const xyz &rhs) const{return *this;}
就是错的了。
为什么呢:
invalid initialization of reference of type ‘xyz&’ from expression of
type ‘const xyz’
所以,这个例子来验证thrust说的那个implicit conversion to const。
c*******9
发帖数: 6411
8
I see both declaration, which one is better?
const xyz& operator+(const xyz &rhs) const);
const xyz operator+(const xyz &rhs) const;
I am thinking that you should not use const xyz& operator+(const xyz &rhs) const),
return a reference of new value is not a good idea, right?
z****e
发帖数: 2024
9
我这个例子是验证conversion用的。
如果一定要说const& 好不好, 是POD就没事。

const),

【在 c*******9 的大作中提到】
: I see both declaration, which one is better?
: const xyz& operator+(const xyz &rhs) const);
: const xyz operator+(const xyz &rhs) const;
: I am thinking that you should not use const xyz& operator+(const xyz &rhs) const),
: return a reference of new value is not a good idea, right?

d****p
发帖数: 685
10
Yes, generated by compiler.

signature

【在 c*******9 的大作中提到】
: void name(const Person* person)
: So when you call name() via aperson.name(), what happened is
: name(&aperson)
: could you explain where the following come from? it is not in the signature
: , it the following generated by the compiler?
: void name(const Person* person)
: thanks...

1 (共1页)
进入Programming版参与讨论
相关主题
one question about initializaiton listAn object of A automatically converted to an object of B.
[合集] question on item15 in Effective C++Re: VC里面的stl支持是不是很弱?
小问题a string define question (c++)
发现一个有趣的现象。C++ implicit typename的问题
再问一下[合集] 请问-fno-implicit-templates的用处
which func will be called?请叫一个 template class constructor 的问题
问题c++ question
c++ operator overloading questionconst 指针类型转换
相关话题的讨论汇总
话题: const话题: xyz话题: person话题: name话题: aperson