#include
using namespace std;
class C{
public:
virtual ~C(){}A dummy function must be added, otherwise the following error will be reported, the
};
class A:public C{
};
class B:public C{
};
int main(){
C* pa=new A();
B* pb=dynamic_cast<B*>(pa);
if(pb)
cout<<"success!";
else
cout<<"fail!";
}
[Error] cannot dynamic_cast’pa’ (of type’class C*’) to type’class B*’ (source type is not polymorphic)
1. The base class pointer pa points to the subclass object. Class A and Class B are actually irrelevant, so two unrelated classes do dynamic_cast, pb is null, so the final operation result is: fail!
2. Dynamic conversion classes need to add a virtual function. Any virtual function can be used.
Similar Posts:
- How to Solve Const variable assignment Error
- Convert Object to List>, avoiding Unchecked cast: ‘java.lang.Object’ to ‘java.util.List
- Qt undefined reference to `vtable for * * * ‘
- [Problems with the use of internal classes] No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclo
- Java error: No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- JAVA ERROR: The public type *** must be defined in its own file***
- Xlua#: How to Call C
- fatal: refusing to merge unrelated histories [How to Solve]
- [Solved] SpringBoot Startup Error: is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)