Connecting Tech Pros Worldwide Forums | Help | Site Map

static_cast vs. traditional type casting

Suzanne Vogel
Guest
 
Posts: n/a
#1: Jul 19 '05
** 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.


Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 19 '05

re: static_cast vs. traditional type casting


On Fri, 29 Aug 2003 14:37:01 -0400, Suzanne Vogel <suzanne_e_vogel@hotmail.com> wrote:
[color=blue]
>** 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[/color]

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.


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

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++.

Ron Natalie
Guest
 
Posts: n/a
#3: Jul 19 '05

re: static_cast vs. traditional type casting



"Suzanne Vogel" <suzanne_e_vogel@hotmail.com> wrote in message news:3f4f9d4b$1_1@news.unc.edu...[color=blue]
> ** Isn't the 'static_cast' operator the same as traditional type
> casting? ie, Aren't the following ways of getting b1, b2 the same?[/color]

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

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


Alf P. Steinbach
Guest
 
Posts: n/a
#4: Jul 19 '05

re: static_cast vs. traditional type casting


On Fri, 29 Aug 2003 15:09:02 -0400, "Ron Natalie" <ron@sensor.com> wrote:
[color=blue]
>
>"Suzanne Vogel" <suzanne_e_vogel@hotmail.com> wrote in message news:3f4f9d4b$1_1@news.unc.edu...[color=green]
>> ** Isn't the 'static_cast' operator the same as traditional type
>> casting? ie, Aren't the following ways of getting b1, b2 the same?[/color]
>
>No. The C-style cast may do static, reinterpret, const, or combinations there of, plus
>one that there is no new style equivelent to.[/color]

Which one is that?

Ron Natalie
Guest
 
Posts: n/a
#5: Jul 19 '05

re: static_cast vs. traditional type casting



"Alf P. Steinbach" <alfps@start.no> wrote in message news:3f4fa692.428850937@News.CIS.DFN.DE...[color=blue]
> On Fri, 29 Aug 2003 15:09:02 -0400, "Ron Natalie" <ron@sensor.com> wrote:
>[color=green]
> >
> >"Suzanne Vogel" <suzanne_e_vogel@hotmail.com> wrote in message news:3f4f9d4b$1_1@news.unc.edu...[color=darkred]
> >> ** Isn't the 'static_cast' operator the same as traditional type
> >> casting? ie, Aren't the following ways of getting b1, b2 the same?[/color]
> >
> >No. The C-style cast may do static, reinterpret, const, or combinations there of, plus
> >one that there is no new style equivelent to.[/color]
>
> Which one is that?
>[/color]
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.


Alf P. Steinbach
Guest
 
Posts: n/a
#6: Jul 19 '05

re: static_cast vs. traditional type casting


On Fri, 29 Aug 2003 15:19:20 -0400, "Ron Natalie" <ron@sensor.com> wrote:
[color=blue]
>
>"Alf P. Steinbach" <alfps@start.no> wrote in message news:3f4fa692.428850937@News.CIS.DFN.DE...[color=green]
>> On Fri, 29 Aug 2003 15:09:02 -0400, "Ron Natalie" <ron@sensor.com> wrote:
>>[color=darkred]
>> >
>> >"Suzanne Vogel" <suzanne_e_vogel@hotmail.com> wrote in message news:3f4f9d4b$1_1@news.unc.edu...
>> >> ** 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.[/color]
>>
>> Which one is that?
>>[/color]
>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.[/color]

Are you sure this is not simply a reinterpret_cast?

In other words, does it work on references?

Closed Thread


Similar C / C++ bytes