dynamic-cast Questions

4

Solved

Consider the problem of getting an object as argument and printing its type: #include <iostream> class A { }; class B : public A { }; class C : public A { }; class D : public C, public B {...
Bhayani asked 5/4, 2012 at 23:12

3

I have my custom little OOP-esque inheritance functionality, something like this: // base class struct BaseTag; typedef struct { int (*DoAwesomeStuff)(struct BaseTag* pInstance); } S_BaseVtable; ...
Ietta asked 31/3, 2023 at 20:51

4

Solved

I'm curious to know what happens when compiling code with a dynamic cast whith RTTI disabled (either with -fno-rttion GCC or with /GR- on visual studio). Does the compiler "falls back" to static_c...
Chaliapin asked 7/10, 2011 at 12:11

6

Solved

Before reading the question: This question is not about how useful it is to use dynamic_cast. Its just about its performance. I've recently developed a design where dynamic_cast is used a lot. Whe...
Reimer asked 29/10, 2010 at 10:14

1

Solved

We have the general form of dynamic_cast: dynamic_cast < new-type > ( expression ) I am specifically confused about the bold part of this rule (5a): 5: If expression is a pointer or refere...
Deservedly asked 8/4, 2021 at 14:57

4

Solved

Motivation The C++ Core Guidelines recommends using dynamic_cast when "class hierarchy navigation is unavoidable." This triggers clang-tidy to throw the following error: Do not use static...

5

I am being very confused about dynamic_cast. Material from C++ Primer and cppreference(rule 5) can't help me understand. (cppreference is way much harder than the book and I read them both very car...
Blame asked 28/9, 2018 at 13:57

1

I have a class Base and a derived class Derived. A function reads objects from a file descriptor and returns Base objects: std::unique_ptr<Base> readNextThing(); And in some place I need to ...
Disburse asked 21/9, 2020 at 14:1

6

Solved

According to this, void* has no RTTI information, therefore casting from void* is not legal and it make sense. If I remember correctly, dynamic_cast from void* was working on gcc. Can you please...
Punkie asked 9/11, 2010 at 6:36

4

I noticed that if I use C style casting (or reinterpret_cast) in the code below, I get a segmentation fault exception but if I use a dynamic_cast, it is OK. Why is this? Since I'm sure that the poi...

3

Solved

Is this code legal? class Base1 { }; class Base2 { public: virtual ~Base2() { if (!dynamic_cast<Base1*>(this)) std::cout << "aaaa" << std::endl; } Base2() { } }; class My...

6

Solved

(Please no advise that I should abstract X more and add another method to it.) In C++, when I have a variable x of type X* and I want to do something specific if it is also of type Y* (Y being a s...
Braces asked 16/10, 2010 at 14:7

2

Solved

I noticed that compiler will optimize out some dynamic_cast when the following operation is non-polymorphic, for example the following code: #include <iostream> using namespace std; struc...
Souse asked 27/9, 2019 at 7:18

4

Solved

I have a class hierarchy as follows: class BaseSession : public boost::enable_shared_from_this<BaseSession> class DerivedSessionA : public BaseSession class DerivedSessionB : public BaseSess...
Julius asked 15/10, 2014 at 8:1

6

Solved

I found myself in a situation where I know what type something is. The Type is one of three (or more) levels of inheritance. I call factory which returns B* however T is either the highest level of...
Octroi asked 17/10, 2011 at 4:17

2

Look at this little snippet: struct A { virtual ~A() { } }; struct B { }; bool fn() { A *volatile a = new A; return dynamic_cast<B *>(a); } Is the compiler allowed to remove the dyna...
Hurter asked 27/9, 2018 at 21:38

1

Solved

I can understand why dynamic_cast does work in this case : #include <iostream> struct A{ virtual ~A() = default; }; struct B { virtual ~B() = default; }; struct C : A, B{}; void f(cons...
Chaps asked 4/9, 2018 at 16:48

10

Solved

I am quite confused with the dynamic_cast keyword in C++. struct A { virtual void f() { } }; struct B : public A { }; struct C { }; void f () { A a; B b; A* ap = &b; B* b1 = dynamic_cas...
Yap asked 12/2, 2010 at 16:10

2

What is side-cast/cross-cast in Dynamic_cast in C++. Can someone explain with an example? #include <iostream> using namespace std; class A { virtual void fn(){} }; class B:public A { };...
Phenetidine asked 12/3, 2016 at 15:58

4

Solved

So I've got a simple task to do. There are 3 classes derived from one base class. They're quite simple and will be provided below. What I need to do is create a new class called PolymorphicAnimal, ...
Coact asked 3/7, 2018 at 9:20

4

Solved

class BASE { public: virtual ~BASE() {} void lamp() { cout << "\nBASE CLASS"; } }; class DERIVED : public BASE { public: void fun(); }; void DERIVED::fun() { cout << "\nDERIVED ...
Colligate asked 30/8, 2017 at 1:54

1

Consider this class hierarchy: struct Animal { virtual ~Animal(); }; struct Cat : virtual Animal {}; struct Dog final : virtual Animal {}; My understanding is that putting final on class Dog ens...
Irena asked 11/6, 2017 at 23:16

2

Solved

Could someone explain in as simple terms as possible (or as simple as you would like) what qobject_cast is, what it does and why we would need to cast one class type to another? Like, I get typeca...
Tuantuareg asked 16/5, 2017 at 7:5

2

Solved

I have some code with a generic interface in which I need to cast up and down. I'm trying to convert to smart pointers now, but am running into some errors. The code below reproduces the problem. I...
Oneeyed asked 28/4, 2017 at 14:7

13

Solved

I would like to do dynamic casting for a Java variable, the casting type is stored in a different variable. This is the regular casting: String a = (String) 5; This is what I want: String th...
Decimeter asked 24/1, 2010 at 14:11

© 2022 - 2024 — McMap. All rights reserved.