Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > alternative to casting to derived type

Reply
Thread Tools

alternative to casting to derived type

 
 
jimmy
Guest
Posts: n/a
 
      05-18-2005
Hi,

Please forgive me if this is elementary. I can't seem to find the
solution anywhere.

// foo() is only declared/defined in Derived
Base* base = new Derived();
((Derived*)base)->foo();

Given that I would like to use a Base*; is casting the only solution?

-J

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      05-18-2005
jimmy wrote:

> Hi,
>
> Please forgive me if this is elementary. I can't seem to find the
> solution anywhere.
>
> // foo() is only declared/defined in Derived
> Base* base = new Derived();
> ((Derived*)base)->foo();
>
> Given that I would like to use a Base*; is casting the only solution?


Yes. But don't, I repeat, DON'T use C style casts for it. Use dynamic_cast
or static_cast.

 
Reply With Quote
 
 
 
 
ben
Guest
Posts: n/a
 
      05-18-2005

> Yes. But don't, I repeat, DON'T use C style casts for it. Use dynamic_cast
> or static_cast.


dynamic_cast may NOT work if Base has no virtual function at all.
static_cast is a better alternative.

ben


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-18-2005
ben wrote:
>
> > Yes. But don't, I repeat, DON'T use C style casts for it. Use dynamic_cast
> > or static_cast.

>
> dynamic_cast may NOT work if Base has no virtual function at all.


But in situations like the one the OP is in, the Base class
usually has at least one virtual function: the destructor.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-18-2005
ben wrote:
>
> > Yes. But don't, I repeat, DON'T use C style casts for it. Use dynamic_cast
> > or static_cast.

>
> dynamic_cast may NOT work if Base has no virtual function at all.


Besides: Can you quote where this is nailed down in the C++ standard?

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      05-18-2005

> But in situations like the one the OP is in, the Base class
> usually has at least one virtual function: the destructor.


How do you know? I've seen plausible designs where no virtual function is
involved in inheritance.

ben


 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      05-18-2005

> Besides: Can you quote where this is nailed down in the C++ standard?
>


hmm, I don't have the standard (can't afford it) but if you try to
dynamic_cast a non polymorphic type the compiler's not gonna do it for you.

ben


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-18-2005
ben wrote:
>
> > But in situations like the one the OP is in, the Base class
> > usually has at least one virtual function: the destructor.

>
> How do you know? I've seen plausible designs where no virtual function is
> involved in inheritance.


Because the OP posted:

> Base* base = new Derived();
> ((Derived*)base)->foo();


And since I assume that he is a good guy he will eventuall delete
the object. And for that he needs a virtual destructor or he
has undefined behaviour in his program.

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      05-18-2005

> Because the OP posted:
>
> > Base* base = new Derived();
> > ((Derived*)base)->foo();


without virtual destructors:

Derived* ptr_to_derived = reinterpret_cast<Base*>(base);
delete ptr_to_derived;


 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      05-18-2005
oops, I wonder why I wrote "reinterpret_cast"...and it is an ugly example
just to show it would work. It is not what I meant "plausible design" though

ben

> Ok, besides the fact that it's wrong (*): Do you really think this is a
> plausible design?
>
> (*) You have to use a static_cast, not a reinterpret_cast.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem type-casting to a derived class Rui Maciel C++ 9 05-13-2010 06:56 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
casting an vector::iterator to a derived type to the base class propokergrad@gmail.com C++ 4 03-30-2007 02:54 AM
Type casting a base class to a derived one? Frederic Rentsch Python 11 01-24-2007 07:55 PM



Advertisments