locked
dynamic_cast compilation error RRS feed

  • Question

  • First let me apologize for posting this under Visual c#.  When I selected Visual Studio Languages in Forum Category the only options in Forum were Visual Basic and Visual c#.  What happened to Visual c++?

    I have a project where I have a base class (Base) and several subclasses (Subclass1, 2, etc).  I have a pointer to a Base object Base * pB.  This pointer gets initialized to a subclass as for example pB = new Subclass1.  I need to check the class at runtime and I have something like Subclass1 * sc1 = dynamic_cast<Subclass1*>(pB).  If pB is not NULL I know it's Subclass1.  All this works fine.  

    Now I have another project where I do the same thing.  But when I do Subclass1 * sc1 = dynamic_cast<Subclass1*>(pB) I get a compile error saying that Base is not a polymorphic type.  Why would this cause a compilation error in one project and not the other when I do basically the same thing in each?

    • Moved by CoolDadTx Wednesday, November 11, 2020 2:59 PM Not C# related
    Tuesday, November 10, 2020 11:26 PM

Answers

  • Just a quick check.  If the base class does not have virtual functions the compiler will issue error C2683: 'dynamic_cast': 'Base' is not a polymorphic type
    • Marked as answer by lfvoydfv Thursday, November 12, 2020 11:53 PM
    Wednesday, November 11, 2020 5:11 AM

All replies