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
-2
votes
2 answers

C++ downgrade parent pointer to child pointer used as function parameter

I want use child pointer in place of parent parameter in some function. Here is simplified code #include #include using namespace std; class Parent { private: string value; public: void SetValue(const…
JalalJaberi
  • 2,417
  • 8
  • 25
  • 41
-2
votes
1 answer

Dont know what type of class I have after doing a

I have this structure: class IIterator : public ICollectible{}; class A: public ICollectible{}; class b: public A{}; class c: public A{}; class d: public A{}; When I do something like this IIterator* it = colection->getIterator(); whatType* db =…
-2
votes
1 answer

Counting elements in a list with different objects given a property

So i got this two classes and i want to count the number of FlowersGarden objects with the specie rose in my list: class Garden { private: string owner; double lenght, width; public: Garden(string ow, double l, double w) { …
SuperChit
  • 11
  • 3
-2
votes
1 answer

Casting a pointer and a reference benefits?

Suppose I have the following two statements base* b = ...; fnl * c = dynamic_cast(b); //Statement A fnl& d = dynamic_cast(*b); //Statement B I wanted to know exactly what the difference between statement A and statement B is. I…
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
-2
votes
2 answers

Dynamic_cast: Two classes that have the almost same methods

Let's say we have two classes: class A: { A(int a); // c'tor virtual ~A(); // d'tor FirstMethod(...); SecondMethod(...); } class B:public A { B(int a); // c'tor ~B(); // d'tor FirstMethod(...); SecondMethod(...); } The…
-2
votes
2 answers

Declaring variable with dynamic class in Objective C

As a logic continuing of the question: Objective-C dynamic_cast? From there we learned we can do the following: MyClass *safeObject = objc_dynamic_cast(originalObject, MyClass); Lets suppose the hierarchy: A -> B, A -> C, A -> D. The function…
Dumoko
  • 2,614
  • 3
  • 25
  • 34
-2
votes
2 answers

Alternatives to downcasting when implementations have unique methods

I have the following Issue. Where I have to cast inside of the makeLeftTurnMethod... this looks very ugly to me.. Is there a way where I don't have to do this? public interface Car(){ public void turnRight(); public void turnLeft(); public…
-3
votes
1 answer

Dynamic cast in class hierarchy with templates

in my project if I define base class Base_Dialog as non template and then try to assign 'caller' in already_exists_ it works in the way expected but if I make Base_Dialog as a template class then the algorithm 'already_exists_' (unchanged) will not…
user336635
  • 2,081
  • 6
  • 24
  • 30
-3
votes
2 answers

In C++, is a "public unsigned int type" in base class, a faster alternative to dynamic_cast?

In C++, "dynamic_cast" being slow is a known fact. I thought of following simple way of knowing the type of an object in a hierarchy. Could someone please explain if this could be slower than dynamic_cast? And if not, then why isn't it a common…
-3
votes
2 answers

why this error happens when I use dynamic_cast?

I want to downcast a object but it causes 'cannot dynamic_cast' error.. (source is not of class type) what is the problem?? this is my code, class A{...} class B: public A{...} A& x; dynamic_cast(&x))!=0 //error here (source is not of class…
sol
  • 11
  • 2
-3
votes
1 answer

Downcast object which was instantiated with parent class

I have a parent-child inheritance structure. I have an object that was instantiated(new) by parent class. I want to downcast this object to child class. I need a automatic routine like casting because parent class has a lot of properties and copying…
hamed
  • 471
  • 1
  • 9
  • 22
-3
votes
1 answer

Using dynamic_cast in c++

I have 4 classes in c++ . Animal is the super class were Snake and Tiger inherit from Animal but also inherit from Dangerous Animal . I have implemented a function to check if Snake or tiger is an instance of DangerousAnimal . However I am finding…
-3
votes
1 answer

type casting is going somewhere wrong in VC++

I have a class _PDevice which is implemented in PDevice.cpp and declared in PDevice.h Also, in PDevice.h, I have added: typedef QSharedPointer<_PDevice> DDevice; Now, there is another class QLDevice which inherits _PDevice QLDevice also has a…
-3
votes
4 answers

Dynamic_cast failing

Suppose the code is as such: #include using namespace std; class dog { public: virtual ~dog() { } }; class yellowdog : public dog { int age; public: void bark() { cout << "woof." << endl;} }; int main() { dog *pd…
lakshmen
  • 28,346
  • 66
  • 178
  • 276
-4
votes
1 answer

Convert by "dynamic_cast" between points to classes

I tried to convert by "dynamic_cast" in the following way: #include class Shape { //.... }; class Square: Shape { //.... }; class Circle: Shape { //.... }; int main() { Circle cr; Shape* sh = &cr; // ERROR1 …
Mathing
  • 41
  • 5
1 2 3
41
42