W
Wayne Shu
Hi everyone,
Today I meet a problem about virtual function. Consider the
following little program.
#include <iostream>
class base
{
int i;
public:
virtual void print() { std::cout << "base" << std::endl; }
virtual ~base() {}
};
class derived : public base
{
public:
virtual void print() { std::cout << "derived" << std::endl; }
};
int main()
{
base b;
b::~base();
new (&b) derived;
(&b)->print(); // (1)
base *p = &b;
p->print();
return 0;
}
I have try the VC2005 Express Edition and GCC 3.4.2
and the result is :
base
derived
Why (1) statement print "base"?
thanks
Best Regards.
Today I meet a problem about virtual function. Consider the
following little program.
#include <iostream>
class base
{
int i;
public:
virtual void print() { std::cout << "base" << std::endl; }
virtual ~base() {}
};
class derived : public base
{
public:
virtual void print() { std::cout << "derived" << std::endl; }
};
int main()
{
base b;
b::~base();
new (&b) derived;
(&b)->print(); // (1)
base *p = &b;
p->print();
return 0;
}
I have try the VC2005 Express Edition and GCC 3.4.2
and the result is :
base
derived
Why (1) statement print "base"?
thanks
Best Regards.