s**i 发帖数: 381 | 1 Suppose I have the following class:
class test{
public:
void func() const
{
cout<<"hello,func const"<
}
void func()
{
cout<<"hello,func"<
}
};
when A is an object of test.
What will A.func() give me?
My test says always the "hello,func" but I don't know why.
Thanks | P*****x 发帖数: 72 | 2 what if you reverse the sequence of definition?
I guess this is compiler specific. Your object is
not changed in either case and the declaration of
the second one is wrong.
【在 s**i 的大作中提到】 : Suppose I have the following class: : class test{ : public: : void func() const : { : cout<<"hello,func const"<: } : void func() : { : cout<<"hello,func"<
| K*****n 发帖数: 65 | 3 when a member function is called, "this" pointer is implicitly passed as
argument.It is part of function signature. Therefore
void func() const;//say "*this" will not be changed.
void func(); //"*this" may or may not be changed.
void main()
{
test a;
a.func(); //call void func();
((const A)a).func(); //call void func() const;
(*(const A *)&a).func(); //call void func() const;
}
【在 s**i 的大作中提到】 : Suppose I have the following class: : class test{ : public: : void func() const : { : cout<<"hello,func const"<: } : void func() : { : cout<<"hello,func"<
|
|