Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6923549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:32:58+00:00 2026-05-27T10:32:58+00:00

Is it possible to have inheritance with no virtual methods? The compiler is saying

  • 0

Is it possible to have inheritance with no virtual methods? The compiler is saying that the following code is not polymorphic.

Example:

class A {
public:
    int a;
    int getA(){return a;};
}


class B : public A {
public:
    int b;
    int getB(){return b;};
}

In another class we are trying to downcast from an A object to a B object:

 A *a = ...;
 B *b = dynamic_cast<B*>(a)

but this gives the following compile-time error:

 cannot dynamic_cast ... (source type is not polymorphic)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:32:58+00:00Added an answer on May 27, 2026 at 10:32 am

    Syntax errors non-withstanding, you cannot dynamic_cast a non-polymorphic type. static_cast is the cast you would use in this case, if you know that it is in fact an object of the target type.

    The reason why: static_cast basically has the compiler perform a check at compile time “Could the input be cast to the output?” This is can be used for cases where you are casting up or down an inheritance hierarchy of pointers (or references). But the check is only at compile time, and the compiler assumes you know what you are doing.

    dynamic_cast can only be used in the case of a pointer or reference cast, and in addition to the compile time check, it does an additional run time check that the cast is legal. It requires that the class in question have at least 1 virtual method, which allows the compiler (if it supports RTTI) to perform this additional check. However, if the type in question does not have any virtual methods, then it cannot be used.

    The simplest case, and probably worthwhile if you’re passing pointers around like this, is to consider making the base class’s destructor virtual. In addition to allowing you to use dynamic cast, it also allows the proper destructors to be called when a base class pointer is deleted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Datatemplate inheritance I have several data types that are not subclasses, nor
Is it possible to have multiple inheritance in XML schemas, if so how? Example
Possible Duplicate: Can Super deal with multiple inheritance? Python inheritance? I have a class
Is non-virtual inheritance possible in Python, or do I have to use composition? I
Possible Duplicate: Inherit CSS class Does CSS support inheritance ? Assuming we have: .base
Possible Duplicate: Virtual Functions and Performance C++ Is this correct, that class member function
I have the following two interfaces, which are not part of an inheritance hierarchy.
Multiple Inheritance in ActionScript 3? Is it possible? I have read somewhere that it
I have the following multi-table inheritance situation: from django.db import Models class Partner(models.Model): #
Is it possible to have a <div> simultaneously (1) not take up all available

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.