dynamic-cast

dynamic_cast issues: typeid object is not equal, but name is equal

被刻印的时光 ゝ 提交于 2021-02-16 07:49:11
问题 I found that dynamic_cast didn't work in a situation where I expected it to, and looking at the typeid of the objects at runtime has made the situation even less clear. I just want a cast from base to derived, and I can't figure out why it's not working. I have a class structure something like this: class BoundaryCondition { public: virtual void DoSomething() = 0; virtual ~BoundaryCondition() { /* * */ } } class ReflectingBc : BoundaryCondition { public: virtual void DoSomething(); } class

How does dynamic_cast work?

大城市里の小女人 提交于 2021-02-05 20:28:39
问题 If you had the following: class Animal{}; class Bird : public Animal{}; class Dog : public Animal{}; class Penguin : public Bird{}; class Poodle : public Dog{}; Does dynamic_cast just check if one class is a derived class of another, or if one class is a base class of another? So if I had: Bird* bird; Animal* animal; bird = dynamic_cast<Animal*>(bird); animal = dynamic_cast<Bird*>(animal); bird would now point to an Animal class, so that I can use bird->some_function(); and it will call the

How does dynamic_cast work?

丶灬走出姿态 提交于 2021-02-05 20:27:04
问题 If you had the following: class Animal{}; class Bird : public Animal{}; class Dog : public Animal{}; class Penguin : public Bird{}; class Poodle : public Dog{}; Does dynamic_cast just check if one class is a derived class of another, or if one class is a base class of another? So if I had: Bird* bird; Animal* animal; bird = dynamic_cast<Animal*>(bird); animal = dynamic_cast<Bird*>(animal); bird would now point to an Animal class, so that I can use bird->some_function(); and it will call the

How does dynamic_cast work?

拜拜、爱过 提交于 2021-02-05 20:25:58
问题 If you had the following: class Animal{}; class Bird : public Animal{}; class Dog : public Animal{}; class Penguin : public Bird{}; class Poodle : public Dog{}; Does dynamic_cast just check if one class is a derived class of another, or if one class is a base class of another? So if I had: Bird* bird; Animal* animal; bird = dynamic_cast<Animal*>(bird); animal = dynamic_cast<Bird*>(animal); bird would now point to an Animal class, so that I can use bird->some_function(); and it will call the

How does dynamic_cast work?

孤人 提交于 2021-02-05 20:25:38
问题 If you had the following: class Animal{}; class Bird : public Animal{}; class Dog : public Animal{}; class Penguin : public Bird{}; class Poodle : public Dog{}; Does dynamic_cast just check if one class is a derived class of another, or if one class is a base class of another? So if I had: Bird* bird; Animal* animal; bird = dynamic_cast<Animal*>(bird); animal = dynamic_cast<Bird*>(animal); bird would now point to an Animal class, so that I can use bird->some_function(); and it will call the

Why is the dynamic_cast allowed to yield a null-pointer for polymorphic classes when the destination pointer is not of the type of a base class?

陌路散爱 提交于 2020-01-24 09:00:11
问题 Consider the following program #include <iostream> #include <iomanip> struct A { }; struct C { }; int main() { C *pc = nullptr; A *pa1 = dynamic_cast<A *>( pc ); std::cout << "pa1 == nullptr is " << std::boolalpha << ( pa1 == nullptr ) << '\n'; A *pa2 = pc; std::cout << "pa2 == nullptr is " << std::boolalpha << ( pa2 == nullptr ) << '\n'; } For the both pointer declarations, pa1 and pa2, the compiler reports an error that such an initialization is not allowed. For example the clang HEAD 10.0

dynamic_cast doubt from C++/Stroustrup : converting to protected base class

百般思念 提交于 2020-01-23 10:11:05
问题 I know that following code gives compilation error : class A{ public : virtual void name(){cout<<typeid(this).name()<<endl;}; }; class B:protected A{public : virtual void name(){cout<<typeid(this).name()<<endl;};}; void foo(B* b) { A * a = dynamic_cast<A*>(b); //Error : 'A' is an inaccessible base of 'B' return; } But then why in the C++ Stroustrup book (15.4.1) he writes class BB_ival_slider:public Ival_slider,protected BBslider{ //... }; void f(BB_ival_slider*p) { // ok BBslider* pbb2 =

How to identify failed casts using dynamic_cast operator?

我们两清 提交于 2020-01-20 03:59:45
问题 Scott Meyer in his book Effective C++ says dynamic_cast is used to perform safe casts down or across an inheritance hierarchy. That is, you use dynamic_cast to cast pointers or references to base class objects into pointers or references to derived or sibling base class objects in such a way that you can determine whether the casts succeeded. Failed casts are indicated by a null pointer (when casting pointers) or an exception (when casting references). I would like to get two code snippet

Well-known solution for avoiding the slowness of dynamic_cast?

守給你的承諾、 提交于 2020-01-12 08:24:32
问题 I needed run-time polymorphism, so I used dynamic_cast . But now I had two problems -- dynamic_cast was extremely slow ! (Scroll down for benchmark.) Long story short, I ended up solving the problem this way, using static_cast : struct Base { virtual ~Base() { } virtual int type_id() const = 0; template<class T> T *as() { return this->type_id() == T::ID ? static_cast<T *>(this) : 0; } template<class T> T const *as() const { return this->type_id() == T::ID ? static_cast<T const *>(this) : 0; }