Does C++ Spec allow static_cast of polymorphic objects?

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
 
K

Kai-Uwe Bux

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,

Yes, see [5.2.9/8]:

An rvalue of type ?pointer to cv1 B?, where B is a class type, can be
converted to an rvalue of type ?pointer to cv2 D?, where D is a class
derived (clause 10) from B, if a valid standard conversion from ?pointer
to D? to ?pointer to B? exists (4.10), cv2 is the same cv-qualification
as, or greater cv-qualification than, cv1, and B is not a virtual base
class of D. The null pointer value (4.10) is converted to the null pointer
value of the destination type. If the rvalue of type ?pointer to cv1 B?
points to a B that is actually a sub-object of an object of type D, the
resulting pointer points to the enclosing object of type D. Otherwise, the
result of the cast is undefined.


The difference to dynamic_cast<> is the last bit: static_cast<> invokes
undefined behavior if the pointee is not of the correct derived type. In
that case, dynamic_cast<> will return the null pointer. Therefore,
dynamic_cast<> can be used for runtime type identification in a way that
of is it just something that will vary from compiler to compiler?

No.



Best

Kai-Uwe Bux
 
A

Andrey Tarasevich

I saw that some web sites say that static_cast cannot be used to
downcast polymorphic classes.

That's incorrect. For 'static_cast' it doesn't make any difference
whatsoever whether the class is polymorphic or not.
So I'm wondering now if the C++ spec allows this, of is it just
something that will vary from compiler to compiler?

Yes, C++ allows this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top