G
Guest
#include <iostream>
using namespace std;
class Base
{
public:
Base() {}
~Base() {}
};
class Derived : virtual public Base
{
public:
Derived() {}
~Derived() {}
};
int main()
{
Base *b = new Derived();
Derived *d = (Derived*)b; //line 22. The problem line!
return 0;
}
For the code above, I got the following error message:
downcast.cc: In function `int main()':
downcast.cc:22: error: cannot convert from base `Base' to derived type `
Derived' via virtual base `Base'
That means it is illegal to explicit convert a VIRTUAL base point to the
derived point. (non-virtual is ok) Can somebody explain it? ( The coding
style is bad, please just ignore it. ^_^)
Thanks in advance!
using namespace std;
class Base
{
public:
Base() {}
~Base() {}
};
class Derived : virtual public Base
{
public:
Derived() {}
~Derived() {}
};
int main()
{
Base *b = new Derived();
Derived *d = (Derived*)b; //line 22. The problem line!
return 0;
}
For the code above, I got the following error message:
downcast.cc: In function `int main()':
downcast.cc:22: error: cannot convert from base `Base' to derived type `
Derived' via virtual base `Base'
That means it is illegal to explicit convert a VIRTUAL base point to the
derived point. (non-virtual is ok) Can somebody explain it? ( The coding
style is bad, please just ignore it. ^_^)
Thanks in advance!