Questions tagged [dynamic-cast]

The dynamic_cast conversion allows for safely converting pointers (and references) to classes up, down, and sideways in the inheritance hierarchy.

From cppreference.com:

Safely converts pointers and references to classes up, down, and sideways along the inheritance hierarchy.

dynamic_cast has usage scenarios and limitations. In general terms this amounts to it being valid for casting polymorphic types and it performs run-times checks to ensure that the result is valid for the requested object type.

621 questions
-1
votes
2 answers

Why the constructor is called after a static cast?

This is my class: class AComponent : public nts::IComponent { public: AComponent(const size_t &maxInputs, const size_t &maxOutputs, const size_t &value); AComponent(nts::AComponent &); virtual ~AComponent(); virtual nts::Tristate…
Dimitri Danilov
  • 1,338
  • 1
  • 17
  • 45
-1
votes
1 answer

Why non-virtual function call is successful even after dynamic_cast is failed?

Below is code snippet, #include using namespace std; class A { public: void print() const { cout << "In A::print()\n"; } virtual void show() { cout << "In A::show()\n"; } }; class B : public…
Pravar Jawalekar
  • 605
  • 1
  • 6
  • 18
-1
votes
2 answers

reinterpret_cast and dynamic cast questions

I've read about reinterpret and dynamic cast, but I saw some examples which I have questions about. reinterpret_cast: #include using namespace std; class A { public: void a() { cout << "a"; } }; class B: private…
OferP
  • 373
  • 2
  • 15
-1
votes
1 answer

Error with pointer when using dynamic_cast to detect derived class

I'd like to have some help on an issue I face. I made an inheritance with polymorph program with class Shape and Circle (derived from shape). So I have some code like this main.cpp Shape* shape = new (nothrow)…
Alvin
  • 5
  • 8
-1
votes
1 answer

Is there any way to dynamically cast the item type of a generics collection in Delphi?

Unlike the case with common objects, it is impossible to directly assign generics of different related types in Delphi as follows: Possible (normal objects): var var_1 : TObject; var_2 : MyTObjectSubClass; var_1 := var_2; //Works Not…
QuestionOverflow
  • 649
  • 8
  • 20
-1
votes
1 answer

(Smart) pointers to a base class storing derived objects; is this correct & good practis?

I have the following piece of code where I have a smart pointer of a base class type unique_ptr. But I'd like to store a derived object with it new Bird(). Since it is a pointer this works well. Is this good practise or will I run into…
dani
  • 3,677
  • 4
  • 26
  • 60
-1
votes
2 answers

method chain a derived class method after calling a base class method

With a class and a derivative as shown below, is there a way for the base classes methods to return a object reference of the derived type instead of its own type so syntactically i can chain the methods together? Suppose that object A has methods…
Aage Torleif
  • 1,907
  • 1
  • 20
  • 37
-1
votes
2 answers

C++ dynamic_cast issue

class C1 { public: C1* A; void SomeMethod() { class C2; C2* c2 = dynamic_cast(A); } }; class C2 : public C1 {}; In gcc i'm getting "target is not a pointer or reference to complete type" when dynamic_cast is…
-1
votes
2 answers

Static cast - Cannot cast through virtual inheritance

I was reading about static and dynamic casts along with the differences between them. It states that static_cast cannot cast through virtual inheritance however dynamic cast can. I would appreciate it if someone could clarify what this means…
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
-1
votes
1 answer

c++ static and dynamic type casting

Why static_cast<>() (also known as downcast) doesn't casts types run-time while dynamic_cast<>() (also known as upcast) does? Both are used to advance through class hierarchy. What could be so different between them that it is necessary for…
Ivars
  • 2,375
  • 7
  • 22
  • 31
-1
votes
2 answers

dynamic_cast from Base to Derived

I have a very general question, related to dynamic_cast in c++: Let's assume we have the following classes: class Father{ public: Father(...); // this is constructor //..... //.. Whatever code in here, not important ~…
-1
votes
3 answers

C++ dynamic_cast

class A { }; class B:public A { }; int main() { A a; B b; A *ap = &b; B *bp = dynamic_cast(ap); if(bp!= NULL) cout<<"Pass"<
ranganath111
  • 457
  • 2
  • 7
  • 17
-2
votes
1 answer

issues with dynamic_cast in C++

I'm implementing inhomogeneous linked list (.cpp and .h files are below) in C++. Compiling in Linux using intel compiler with: icpc test.cpp List.cpp -o test gives lots of erros (see below). It seems that the problem originates from the lines…
user3428703
-2
votes
1 answer

Use static_cast to dynamic polymorphism

I used a static_cast to polymorphism. I read that I should use dynamic_cast, but I can't get why. Code works in both way (with static and dynamic cast). Could anybody show me an example with necessary dynamic_cast operator? class Base { …
KeyB0rys
  • 392
  • 5
  • 13
-2
votes
1 answer

why does dynamic_cast return null with multiple level inheritance

While reviewing an inherited project which is to port from an old version of Visual Studio (6) to a new version (2017), we stumbled across this run time error, in that we were getting an unexpected NULL after using dynamic_cast<>() on a base class.…
Gio
  • 4,099
  • 3
  • 30
  • 32