[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Does C++ Spec allow static_cast of polymorphic objects?

kk_oop

11/26/2008 4:08:00 AM

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
2 Answers

Kai-Uwe Bux

11/26/2008 4:23:00 AM

0

kk_oop@yahoo.com wrote:

> 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
static_cast<> cannot.


> of is it just something that will vary from compiler to compiler?

No.



Best

Kai-Uwe Bux

Andrey Tarasevich

11/26/2008 6:45:00 AM

0

kk_oop@yahoo.com wrote:
> 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.

--
Best regards,
Andrey Tarasevich