static_cast vs. traditional type casting

S

Suzanne Vogel

** Isn't the 'static_cast' operator the same as traditional type
casting? ie, Aren't the following ways of getting b1, b2 the same?

// 'Derived' is derived from 'Base'
Derived* d = new Derived();
Base* b1 = static_cast<Base*>(d);
Base* b2 = (Base*)d; // traditional type casting

Such is my understanding from code samples, my own uses, and this:
http://www.cplusplus.com/doc/tutorial/tut5-4.html

Thanks.
 
A

Alf P. Steinbach

** Isn't the 'static_cast' operator the same as traditional type
casting? ie, Aren't the following ways of getting b1, b2 the same?

// 'Derived' is derived from 'Base'
Derived* d = new Derived();
Base* b1 = static_cast<Base*>(d);
Base* b2 = (Base*)d; // traditional type casting

In this particular case the two give equivalent effect.

Generally the C-style cast will employ whatever combination of
static_cast, const_cast and reinterpret_cast necessary to achieve
the specified type conversion.

Often the programmer does not understand _what_ the effect is,
so to the degree casting is unavoidable: at least avoid the C-style
cast, which is only there for backward compatibility.


Such is my understanding from code samples, my own uses, and this:
http://www.cplusplus.com/doc/tutorial/tut5-4.html

Nope. I don't think tutorial is a particularly bad one, but it was
written in a time when code had to be "compromised" to accomodate
the then current compilers. So, for example,


#include <iostream.h>


in that tutorial (even on the page you referred to) is non-standard.

I wish the author could have pointed that out, not give the impression
that this was a tutorial about standard C++.
 
R

Ron Natalie

Suzanne Vogel said:
** Isn't the 'static_cast' operator the same as traditional type
casting? ie, Aren't the following ways of getting b1, b2 the same?

No. The C-style cast may do static, reinterpret, const, or combinations there of, plus
one that there is no new style equivelent to.
// 'Derived' is derived from 'Base'
Derived* d = new Derived();
Base* b1 = static_cast<Base*>(d);
Base* b2 = (Base*)d; // traditional type casting

The only time the latter two lines would not be equivelent are if Base is not
publically inheritted.
 
A

Alf P. Steinbach

No. The C-style cast may do static, reinterpret, const, or combinations there of, plus
one that there is no new style equivelent to.

Which one is that?
 
R

Ron Natalie

Alf P. Steinbach said:
Which one is that?
I alluded to it in the rest of the message. A C-style cast will let you convert to an
inaccessible base class:

class B1 { //...
};
class B2 {
//...
};

class Derived : B1, B2 { } // note non-public bases

Derived d;

B2* bp = (B2*) &d;

There's no way to do that with any of the new style casts.
 
A

Alf P. Steinbach

I alluded to it in the rest of the message. A C-style cast will let you convert to an
inaccessible base class:

class B1 { //...
};
class B2 {
//...
};

class Derived : B1, B2 { } // note non-public bases

Derived d;

B2* bp = (B2*) &d;

There's no way to do that with any of the new style casts.

Are you sure this is not simply a reinterpret_cast?

In other words, does it work on references?
 

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

Staff online

Members online

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top