placement new
发信人: xgYao (小哥), 信区: CPlusPlus
标 题: 请教个腾讯的考题
发信站: 水木社区 (Mon Nov 5 14:48:30 2007), 站内
class base{
public:
virtual void f() {
cout<<"base::f()"<<endl;
}
};
class derived: public base{
public:
void f() {
cout<<"derived::f()"<<endl;
}
};
void main()
{
base b;
(&b)->f();
(&b)->~base();
new(&b) derived;
(&b)->f();
base *pb = &b;
pb->f();
}
输出结果是:
base::f()
base::f()
derived::f()
其中第二个结果挺困惑的,难道不是derived::f()么?
标 题: 请教个腾讯的考题
发信站: 水木社区 (Mon Nov 5 14:48:30 2007), 站内
class base{
public:
virtual void f() {
cout<<"base::f()"<<endl;
}
};
class derived: public base{
public:
void f() {
cout<<"derived::f()"<<endl;
}
};
void main()
{
base b;
(&b)->f();
(&b)->~base();
new(&b) derived;
(&b)->f();
base *pb = &b;
pb->f();
}
输出结果是:
base::f()
base::f()
derived::f()
其中第二个结果挺困惑的,难道不是derived::f()么?