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

  • Home
  • SEARCH
  • 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 8388147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:24:22+00:00 2026-06-09T18:24:22+00:00

There is a base class A, which is virtual class A { ~virtual A()

  • 0

There is a base class A, which is virtual

class A
{
  ~virtual A() = 0;
};

and more derived classes B, C, D, E…

class B : public A
{
};

class C: public A
{
};

and analogously for other classed D, E… We have a list of A pointers

std::list <A*> a_list;

We remove any element which type is unknown, for example

A *a = a_list.front();

Base on the type of a pointed object we decide, what to do… There more possibilities how to do that:

A) dynamic_cast case

Recasting of a to derived types.

if (dynamic_cast <B*> (a))
{
   //do something (but nothing with a)
}

else if (dynamic_cast <C*> (a))
{
    //do other (but nothing with a)
}

But the dynamic_cast usage indicates a bad design.

B) Additional attribute

Some aditional attribute, for example an object ID is impelemented;

class A
{
  virtual ~A() = 0;
  virtual short getID() = 0;
};

class B : public A
{
  virtual short getID() {return 1;}
};

class C: public A
{
  virtual short getID() {return 2;}
};

So the modified condition

switch ( a->getID())
{
   case 1: // do something (but nothing with a)
   case 2: // do other (but nothing with a)
}

A note:

We do not perform any action directly with the object, but on the basis of its type we do some different computations.

Questions:

1) Is it the case, when we should avoid dynamic_cast?

2) is there any prefereable solution (may be different to presented)?

Thanks for your help.

  • 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-06-09T18:24:23+00:00Added an answer on June 9, 2026 at 6:24 pm

    According to Item 90 in C++ Coding Standards (Amazon): Avoid type-switching (regardless whether you do it with an if-else ladder and dynamic_cast, or a switch statement with getID() function). Prefer instead to rely on polymorphism via virtual functions

    class A
    {
    public:
      ~virtual A() = 0;
    
      void fun()  // non-virtual
      { 
         // main algorithm here
    
         // post-processing step
         post_fun();
      }
    
      virtual void post_fun() = 0;
    };
    
    class B : public A
    {
    public:
       virtual void post_fun() { /* bla */ }
    };
    
    class C: public A
    {
    public:
       virtual void post_fun() { /* meow */ }
    };
    
    A* a = a_list.front();
    a->fun(); // will be resolved at run-time to whatever type a points to
    

    The reason is that having an explicit type-switch is hard to maintain and update. If you get a new derived class from A, you need to update every place where you loop over types. Instead, the compiler will do that automatically for you if you rely on virtual functions.

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

Sidebar

Related Questions

I have created an abstract base class Animal which has public virtual abstract method
I have 2 classes: class Base { public: virtual int Foo(int n); virtual void
I have an abstract virtual base class Foo from which I derive many other
I have an abstract base class from which many classes are derived. I want
I have base-class Base from which is derived Derived1 , Derived2 and Derived3 .
Let's suppose we have a base class which has a virtual method: class BaseClass
I have a base class, which is normal Java class. Three subclasses extend it
I have three tables which are defined as: class User(Base): __tablename__ = 'users' id
I have a code base, in which for Matrix class, these two definitions are
In my project, there is one Base class and there are 3 different derived

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.