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...
|