K
kk_oop
Hi. I saw that some web sites say that static_cast cannot be used to
downcast polymorphic classes. For instance, given
class Base
{
public:
Base(){}
virtual ~Base(){}
int getThree() { return 3; }
};
class Derived : public Base
{
public:
Derived(){}
~Derived(){}
int getFour() { return 4; }
};
This static_cast should fail:
Derived* d = new Derived();
Base* b = d;
Derived* downcast_d;
downcast_d = static_cast<Derived*>(b);
However, when I compile and run this using g++ on MinGW, this works
fine. So I'm wondering now if the C++ spec allows this, of is it just
something that will vary from compiler to compiler?
Thanks!
Ken
downcast polymorphic classes. For instance, given
class Base
{
public:
Base(){}
virtual ~Base(){}
int getThree() { return 3; }
};
class Derived : public Base
{
public:
Derived(){}
~Derived(){}
int getFour() { return 4; }
};
This static_cast should fail:
Derived* d = new Derived();
Base* b = d;
Derived* downcast_d;
downcast_d = static_cast<Derived*>(b);
However, when I compile and run this using g++ on MinGW, this works
fine. So I'm wondering now if the C++ spec allows this, of is it just
something that will vary from compiler to compiler?
Thanks!
Ken