In C++ , without making my destructor virtual Is it still possible to downcast pointers/references of my non-polymorphic base class?
-
Yes *might* be possible, but you might have to use `static_cast` or `reinterpret_cast`. *However*, if the base class is not polymorphic, and doesn't have a virtual destructor, then perhaps inheriting from it is not the correct choice. Maybe you should have the base class as a *member* instead? What is the *actual* problem you want to solve? *Why* do you use inheritance? – Some programmer dude Oct 16 '17 at 12:13
-
So, is it correct to say, that I cannot downcast if my base class is non-polymorphic(no virtual method/destructor) – Adrika Oct 16 '17 at 12:16
-
Using `dynamic_cast` it's not possible. – Some programmer dude Oct 16 '17 at 12:18
-
I don't want to use composition .I am exploring inheritance....Using static_cast/ reinterpret_cast...are sufficient only for compile-time checkability....My code may still crash @ run time as static/reinterpret cast may still allow bad ptr/reference to pass through....so, its safe to assume ... if I want to downcast a Base * bptr to Derived * dptr (all non -polymorphic)...I will always be @ the risk of bad ptr/bad reference..(.unless I use a virtual destructor) – Adrika Oct 16 '17 at 12:27
-
If you inherit from non-polymorphic class - you always at risk. – Artemy Vysotsky Oct 16 '17 at 12:30
-
If you get a crash, then perhaps you should ask about *that* instead? Please [take the SO tour](http://stackoverflow.com/tour), then [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 16 '17 at 12:40
-
I tried to answer, but the software said "No" was not enough characters. – Jive Dadson Oct 16 '17 at 12:56
-
@ArtemyVysotsky - As a general statement that's pure rubbish. There's plenty of use-cases for inheriting from types without run-time polymorphism. – StoryTeller - Unslander Monica Oct 17 '17 at 12:58
1 Answers
Virtual destructor has little to do with downcasting. The goal of making destructor virtual is to allow safe deletion through pointer to base.
Base * ptr = new Derived;
delete ptr; // undefined behavior if Base destructor isn't virtual
Downcasting can be performed using static_cast
, on your own responsibility
void processBase(Base * ptr)
{
// undefined behavior if ptr does not point to Derived
// object or some object that inherits from Derived
Derived * derived = static_cast<Derived *>(ptr);
}
There is also dynamic_cast
that will check if downcast is legal, but it requires that casted expression points (or refers) to a polymorphic object (i.e. object that has at least one virtual function declared or inherited).
5.2.7.6 Otherwise, v shall be a pointer to or an lvalue of a polymorphic type (10.3)
If the type of casted expression is not polymorphic, the program will fail to compile.
To summarize - making destructor virtual will make your class polymorphic, but same will be achieved by declaring any other virtual member function. To use dynamic_cast
you need a polymorphic type.

- 12,283
- 6
- 56
- 83
-
-
It means I have a problem with understanding wording in standard. As they use in this section phrase "v has type pointer to cv2 D" and in this section it is important to distinguish type of expression from type of pointed object, I assumed that the requirement refers full type of pointee. However ideone rejects non polymorphic expression type, so you are right. – Tadeusz Kopec for Ukraine Oct 16 '17 at 13:39