Answered by:
dynamic_cast compilation error

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
-
Hello,
Use https://docs.microsoft.com/en-us/answers/topics/c++.html
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
My GitHub code samples
GitHub page- Proposed as answer by KHURRAM RAHIM Friday, January 1, 2021 7:34 AM
Tuesday, November 10, 2020 11:37 PM -
Greetings lfvoydfv.
You can ask questions about c++ here (I think).
Tuesday, November 10, 2020 11:40 PM -
The Visual C++ forum has been closed. If you would like to see the archived forum it can be found here https://social.msdn.microsoft.com/Forums/en-US/home?forum=vcgeneral
As others have pointed out, questions about C++ should now be posted to the Microsoft Q&A site.
When you post your question to https://docs.microsoft.com/en-us/answers/topics/c++.html please indicate which version of VC++ you are using and include a code snippet that reproduces the issue you describe.
Wednesday, November 11, 2020 3:06 AM -
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 -
That was it, thanks!Thursday, November 12, 2020 11:53 PM