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

Dynamic_cast giving segmentation fault

i know what virtual function and polymorphism is. i am bit confused in understanding my below code. class base { public: virtual void display() { cout <<"display base"; } }; class derived :public…
-1
votes
1 answer

Downcasting and copying a const object

My method receives an object as a parameter and it must be accepted as a base type (though it is sent as a Derived type). I want to make a copy of this object AND down cast it to a derived type, so that I can call a method of the derived type…
TSG
  • 4,242
  • 9
  • 61
  • 121
-1
votes
2 answers

C++ gcc 9.3.0 typeid of derived pointer always return typeid(BaseClass*) but dynamic_cast works fine

There are a few similar posts, here is my minimal case code: bool useDerived=true; BaseClass* maker; if (useDerived) { maker = new DerivedClass(); } else { maker = new BaseClass(); } if (typeid(maker) == typeid(DerivedClass*)) { cerr <<…
Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56
-1
votes
2 answers

dynamic_cast fails when cast a base class to derived class

I have two classes, base class and a derived class. The base class has a virtual method. Here is my test example: class Base { public: virtual void Hello() { cout << "-> Hello Base" << endl; } }; class Derived: public Base { public: void…
adir
  • 15
  • 6
-1
votes
1 answer

How to typecast and create object in runtime in cpp?

I want to first create an object of a parent class and according to some condition create child object of child class and put that into parent object. Now while after passing the object to some function that function need to get access to child…
shanto
  • 47
  • 7
-1
votes
2 answers

my object::collides(object * o) function always returns true, but doesn't do anything

I have a function that returns a boolean. this function when compiled seems to contain nothing, and will always return true while also skipping over all the calls to cout or cin that i put in it. to see what it's actually doing. What is going on and…
TheWired
  • 1
  • 3
-1
votes
1 answer

Dynamic casting Failure in C++

I have the following class hierarchy: class A { virtual void blah() = 0; }; class B { virtual void gah() = 0; }; class C: public A, public B {}; class D: public C { gah() {} blah() {} }; If I do the following: A *something =…
ggobieski
  • 149
  • 1
  • 14
-1
votes
2 answers

error in using dynamic_cast and constructor

#include "stdafx.h" #include using namespace std; class myclass { public: int a; myclass() : a(-1){}; }; class derivedclass : public myclass { public: int b; derivedclass() : b(-2) {}; }; int main() { myclass*…
Taitai
  • 584
  • 2
  • 7
  • 18
-1
votes
2 answers

C++ cast vector template

I want to cast BaseClass to DerivedClass std vector using a template: template vector CastTo(vector &input) { vector output; for (auto obj : input) { output.push_back(dynamic_cast(obj)); …
Sturm
  • 3,968
  • 10
  • 48
  • 78
-1
votes
2 answers

Incomlete object of class

Base * pba = new Derived; Base * pbb = new Base; Even though both are pointers of type Base*, pba actually points to an object of type Derived, while pbb points to an object of type Base. Therefore, when their respective type-casts are performed…
-1
votes
1 answer

opposite operation to dynamic_cast

I have a base class and derivative class, e.g : class Base { public: Base(); virtual doSomthing(); }; class Derivative : class Base { public: Derivative(); virtual doSomthing(); }; I know that if I want to change at runtime from the…
Bizzu
  • 449
  • 3
  • 17
-1
votes
1 answer

dynamic_cast pointer exception catch

#include using namespace std; struct A { virtual void foo() { } }; struct B1 :A { }; int main() { int x = 42; A *a = (A*)&x; try { B1 *b = dynamic_cast(a); } catch (...) { cout <<…
Luk Rob
  • 19
  • 4
-1
votes
1 answer

Dynamic cast failure reasons

Dynamic cast fails in same function where it was working. Dynamic cast is working before calling commitTransaction and fails after commitTransaction. In commitTransaction, copy operator is called where delete and new operations are performed. Any…
Tejas Pawar
  • 690
  • 8
  • 16
-1
votes
1 answer

dynamic_cast macro rescue NULL

I want to use a dynamic_cast intrinsic in my generated C++ code. The macro definition looks like: #define jcast(T, v) (dynamic_cast(v)) Unfortunately, because the code is generated, this situation can occur: foo(jcast(UWiseObject, NULL)); A…
Netherwire
  • 2,669
  • 3
  • 31
  • 54
-1
votes
1 answer

dynamic_cast fails for an object whose dynamic type is the casted type

I am pretty sure this is a compiler bug or something: if two types in different translation units have the same name and derive from nested classes of a template class, dynamic_cast will fail in one of those translation units. In my case, I use two…
Kietz
  • 1,186
  • 11
  • 19