avatar
which func will be called?# Programming - 葵花宝典
s*i
1
Suppose I have the following class:
class test{
public:
void func() const
{
cout<}
void func()
{
cout<}
};
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
avatar
P*x
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<: }
: void func()
: {
: cout<
avatar
K*n
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<: }
: void func()
: {
: cout<
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。