alternative to casting to derived type

J

jimmy

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
 
R

Rolf Magnus

jimmy said:
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.
 
B

ben

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
 
K

Karl Heinz Buchegger

ben said:
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.
 
K

Karl Heinz Buchegger

ben said:
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?
 
B

ben

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
 
B

ben

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
 
K

Karl Heinz Buchegger

ben said:
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.
 
B

ben

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
 
R

Rolf Magnus

ben said:
without virtual destructors:

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

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

Alf P. Steinbach

* jimmy:
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?

Preferably 'foo' should be a virtual function declared in 'Base' and
overridden in 'Derived'.

Failing that -- if 'foo' is not meaningful in 'Base', but you still
need to handle heterogenous collection of 'Base' pointers -- you should
use the visitor pattern.

Finally, if that isn't an option either, then you have a fundamental design
error and should redesign.

A static_cast can technically do the job instead of one of the above, but it
means you're doing assembly-language-level programming instead of C++.

As others have mentioned, the cast you're using is _even worse_ than a
static_cast: it's akin to using an assembly language where you _don't
know_ what machine code will be emitted for an instruction, the assembler
choosing whatever it likes the most at the moment. Old C style casts
in C++ generally means (1) complete newbie, (2) Microsoft employee, or (3)
incompetent but non-MS. (There is one thing C style casts can do that C++
casts can't, but that's advanced.) So if for nothing else, then at least
for your own image, avoid C-style casts, completely.
 
K

Karl Heinz Buchegger

ben said:
without virtual destructors:

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

Just a moment.
Are we fooling around right now or are we talking about
plausible real world programming?
 
R

Ron Natalie

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

It's not very hard, it's nailed down in 5.2.7 where they describe
the dynamic_cast operator. Unless you're converting a null pointer,
converting something to itslef, or doing a derived to base conversion,
the class pointed or referred to SHALL BE POLYMORPHIC
 
R

Ron Natalie

ben said:
without virtual destructors:

Derived* ptr_to_derived = reinterpret_cast<Base*>(base);
delete ptr_to_derived;
That won't compile, perhaps you wanted to cast to Derived*?
Further, a static_cast would be safer.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top