Z
zhangyafei_kimi
I am puzzled by a face test, which source code is like below:
#include <iostream>
using namespace std ;
class base
{
public:
virtual void f() { cout << "base::f()" << endl ; }
};
class derive : public base
{
public:
void f() { cout << "derive::f()" << endl ; }
};
int main()
{
base b ;
(&b) -> f() ;//why this f() is static bound?
(&b) -> ~base() ;
new (&b) derive ;
(&b) -> f() ;//why this f() is static bound?
base *p = &b ;
p->f() ;//it is easy to understand this line
system("pause");
return 0 ;
}
The result of this short program is so peculiar and like this:
base::f()
base::f()
derive::f()
In my opinion, the result of expression '(&b)' is a 'base*' type, so
it will cause a polymorphic behavior. But why '(&b) -> f();' is static
bound?
After referring some authoritative documents, I still can not find the
reason. So I am doubting that the standards did not clearly state this
situation. I am eager to know the reason and the fact.
#include <iostream>
using namespace std ;
class base
{
public:
virtual void f() { cout << "base::f()" << endl ; }
};
class derive : public base
{
public:
void f() { cout << "derive::f()" << endl ; }
};
int main()
{
base b ;
(&b) -> f() ;//why this f() is static bound?
(&b) -> ~base() ;
new (&b) derive ;
(&b) -> f() ;//why this f() is static bound?
base *p = &b ;
p->f() ;//it is easy to understand this line
system("pause");
return 0 ;
}
The result of this short program is so peculiar and like this:
base::f()
base::f()
derive::f()
In my opinion, the result of expression '(&b)' is a 'base*' type, so
it will cause a polymorphic behavior. But why '(&b) -> f();' is static
bound?
After referring some authoritative documents, I still can not find the
reason. So I am doubting that the standards did not clearly state this
situation. I am eager to know the reason and the fact.