470,866 Members | 1,878 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,866 developers. It's quick & easy.

Casting pointers

Hi everyone,

Herb Schildt in "C++, the complete reference" writes:

"In C++, it is illegal to convert one type of pointer into another
without the use of an
explicit type cast. For this reason, the preceding program will not
even compile if
you try to compile it as a C++ (rather than as a C) program. However,
the type of
error described can still occur in C++ in a more roundabout manner."

However, the following works with g++ without any errors:

Derived* pDerived = new Derived();
Base* pBase = pDerived;

What was Herb referring to when he said it is illegal to cast from one
type to another (possibly he meant from one class to an unrelated
class, or from an int to a float)

Taras
Jun 27 '08 #1
12 1411
On 30 huhti, 15:50, Taras_96 <taras...@gmail.comwrote:
What was Herb referring to when he said it is illegal to cast from
one type to another
When you need a cast it's a sign of bad design.
Jun 27 '08 #2
Taras_96 wrote:
Hi everyone,

Herb Schildt in "C++, the complete reference" writes:

Stop right there. Ignore *ANYTHING* you read in that book. Take the
book outside and burn it immediately.
Jun 27 '08 #3
In message <48***********************@news.free.fr>, Michael DOUBEZ
<mi************@free.frwrites
>Krice a écrit :
>On 30 huhti, 15:50, Taras_96 <taras...@gmail.comwrote:
>>What was Herb referring to when he said it is illegal to cast from
one type to another
When you need a cast it's a sign of bad design.

Where have you seen that ?
The use of static_cast<>() is fairly common and secure.
Not always. You can use static_cast to downcast from Base* to Derived*,
for example, but at that point you're telling the compiler you know
better than it does, so you'd better be very sure you really are
pointing to a Derived, or ...
>
The only cast that can yield UB are const_cast<>() and
reinterpret_cast<>() (and of course old C cast style)
and static_cast<>().
>and they should be used with care.
--
Richard Herring
Jun 27 '08 #4
Taras_96 wrote:
Hi everyone,

Herb Schildt in "C++, the complete reference" writes:
This book is known for having quite some errors.
"In C++, it is illegal to convert one type of pointer into another
without the use of an explicit type cast.
That is incorrect.
However, the following works with g++ without any errors:

Derived* pDerived = new Derived();
Base* pBase = pDerived;
Yes, assuming that Derived is a class derived from Base, this code is
correct. This should also work:

void* p = pDerived;
What was Herb referring to when he said it is illegal to cast from one
type to another (possibly he meant from one class to an unrelated
class, or from an int to a float)
Who knows? I would recommend to get a better book.

Jun 27 '08 #5
Taras_96 wrote:
Hi everyone,

Herb Schildt in "C++, the complete reference" writes:

"In C++, it is illegal to convert one type of pointer into another
without the use of an
explicit type cast. For this reason, the preceding program will not
even compile if
you try to compile it as a C++ (rather than as a C) program. However,
the type of
error described can still occur in C++ in a more roundabout manner."

However, the following works with g++ without any errors:

Derived* pDerived = new Derived();
Base* pBase = pDerived;
Well, an object of type Derived *is* an object of type Base. Basic
object-oriented programming.
Jun 27 '08 #6
Juha Nieminen wrote:
Taras_96 wrote:
>Hi everyone,

Herb Schildt in "C++, the complete reference" writes:

"In C++, it is illegal to convert one type of pointer into another
without the use of an
explicit type cast. For this reason, the preceding program will not
even compile if
you try to compile it as a C++ (rather than as a C) program. However,
the type of
error described can still occur in C++ in a more roundabout manner."

However, the following works with g++ without any errors:

Derived* pDerived = new Derived();
Base* pBase = pDerived;

Well, an object of type Derived *is* an object of type Base. Basic
object-oriented programming.
However, an object of type pointer to Derived is *not* an object of type
pointer to Base.

Jun 27 '08 #7

On Apr 30, 9:50 pm, Taras_96 <taras...@gmail.comwrote:
Hi everyone,

Herb Schildt in "C++, the complete reference" writes:

"In C++, it is illegal to convert one type of pointer into another
without the use of an
explicit type cast. For this reason, the preceding program will not
even compile if
you try to compile it as a C++ (rather than as a C) program. However,
the type of
error described can still occur in C++ in a more roundabout manner."

However, the following works with g++ without any errors:

Derived* pDerived = new Derived();
Base* pBase = pDerived;

What was Herb referring to when he said it is illegal to cast from one
type to another (possibly he meant from one class to an unrelated
class, or from an int to a float)

Taras
This kind of casting is called "upcasting". As far as derived class
supports all public functionality of the base class, this kind of
casting is safe and explicit casting is not necessary. But I think
it's a good style if you use static_cast<>() for upcasting. For
downcasting and cross-casting you should use dynamic_cast<>(), but
frankly, these kinds of casting almost always point to bad design.
Jun 27 '08 #8
Rolf Magnus wrote:
Juha Nieminen wrote:
>Taras_96 wrote:
>>Hi everyone,

Herb Schildt in "C++, the complete reference" writes:

"In C++, it is illegal to convert one type of pointer into another
without the use of an
explicit type cast. For this reason, the preceding program will not
even compile if
you try to compile it as a C++ (rather than as a C) program. However,
the type of
error described can still occur in C++ in a more roundabout manner."

However, the following works with g++ without any errors:

Derived* pDerived = new Derived();
Base* pBase = pDerived;
Well, an object of type Derived *is* an object of type Base. Basic
object-oriented programming.

However, an object of type pointer to Derived is *not* an object of type
pointer to Base.
But since the objects they are pointing to are "of the same type", the
casting is valid (although only in the Derived -Base direction).
Jun 27 '08 #9
On Apr 30, 5:07 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
Krice a écrit :
On 30 huhti, 15:50, Taras_96 <taras...@gmail.comwrote:
What was Herb referring to when he said it is illegal to cast from
one type to another
When you need a cast it's a sign of bad design.
Where have you seen that ?
The use of static_cast<>() is fairly common and secure.
The only cast that can yield UB are const_cast<>() and
reinterpret_cast<>() (and of course old C cast style) and they
should be used with care.
Are you saying that:

float* pf ;
MyClass* pc = static_cast< MyClass* >(
static_cast< void* >( pf ) ) ;

is "secure"?

Generally, the only pointer cast that is "common and secure" is
dynamic_cast (and it's not that common). static_cast is "common
and secure" for non-pointer types, in specific circumstances,
e.g.:

int a, b ;
double c = static_cast< double >( a ) + b ;

(In this particular case, I'd still use a C style cast:-).) And
of course, all casts have their uses in special cases.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #10
Juha Nieminen wrote:
Rolf Magnus wrote:
>Juha Nieminen wrote:
>>Taras_96 wrote:
Hi everyone,

Herb Schildt in "C++, the complete reference" writes:

"In C++, it is illegal to convert one type of pointer into another
without the use of an
explicit type cast. For this reason, the preceding program will not
even compile if
you try to compile it as a C++ (rather than as a C) program. However,
the type of
error described can still occur in C++ in a more roundabout manner."

However, the following works with g++ without any errors:

Derived* pDerived = new Derived();
Base* pBase = pDerived;
Well, an object of type Derived *is* an object of type Base. Basic
object-oriented programming.

However, an object of type pointer to Derived is *not* an object of type
pointer to Base.

But since the objects they are pointing to are "of the same type", the
casting is valid (although only in the Derived -Base direction).
The sentence that we're discussing about is: "In C++, it is illegal to
convert one type of pointer into another without the use of an
explicit type cast". And that is wrong. In the above example, the pointers
are of different type, and still they can be converted without a cast.

Jun 27 '08 #11
On 2 mai, 12:06, Michael DOUBEZ <michael.dou...@free.frwrote:
[Wrong answer]
Ignore that

Well. Yes, I had no thought of downcasting this way. You are right.

--
Michael
Jun 27 '08 #12
On May 1, 12:32 am, Rolf Magnus <ramag...@t-online.dewrote:
>
Who knows? I would recommend to get a better book.
Does anyone have any recommendations? I personally find Bjarne
Stroustrup's a bit too technical for a beginner (it serves better as a
reference).

Taras

Jun 27 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jacob Jensen | last post: by
16 posts views Thread by He Shiming | last post: by
8 posts views Thread by rihad | last post: by
35 posts views Thread by ytrama | last post: by
14 posts views Thread by Mirko | last post: by
4 posts views Thread by Xavier Roche | last post: by
9 posts views Thread by Jess | last post: by
9 posts views Thread by Taras_96 | last post: by
101 posts views Thread by Tinkertim | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.