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 858091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:29:47+00:00 2026-05-15T08:29:47+00:00

Suppose I have 3 classes as follows (as this is an example, it will

  • 0

Suppose I have 3 classes as follows (as this is an example, it will not compile!):

class Base
{
public:
   Base(){}
   virtual ~Base(){}
   virtual void DoSomething() = 0;
   virtual void DoSomethingElse() = 0;
};

class Derived1
{
public:
   Derived1(){}
   virtual ~Derived1(){}
   virtual void DoSomething(){ ... }
   virtual void DoSomethingElse(){ ... }
   virtual void SpecialD1DoSomething{ ... }
};

class Derived2
{
public:
   Derived2(){}
   virtual ~Derived2(){}
   virtual void DoSomething(){ ... }
   virtual void DoSomethingElse(){ ... }
   virtual void SpecialD2DoSomething{ ... }
};

I want to create an instance of Derived1 or Derived2 depending on some setting that is not available until run-time.

As I cannot determine the derived type until run-time, then do you think the following is bad practice?…

class X
{
public:
   ....

   void GetConfigurationValue()
   {
      ....
      // Get configuration setting, I need a "Derived1"
      b = new Derived1();

      // Now I want to call the special DoSomething for Derived1
      (dynamic_cast<Derived1*>(b))->SpecialD1DoSomething();      
   }
private:
   Base* b;
};

I have generally read that usage of dynamic_cast is bad, but as I said, I don’t know
which type to create until run-time. Please 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-05-15T08:29:47+00:00Added an answer on May 15, 2026 at 8:29 am

    Using dynamic_cast is not bad practice per se. It’s bad practice to use it inappropriately, i.e. where it’s not really needed.

    It’s also a bad practice to use it this way:

    (dynamic_cast<Derived1*>(b))->SpecialD1DoSomething();  
    

    Reason: dynamic_cast(b) may return NULL.

    When using dynamic_cast, you have to be extra careful, because it’s not guaranteed, that b is actually of type Derived1 and not Derived2:

    void GenericFunction(Base* p)
    {
        (dynamic_cast<Derived1*>(b))->SpecialD1DoSomething();
    }
    
    void InitiallyImplementedFunction()
    {
       Derived1 d1;
       GenericFunction(&d1); // OK... But not for long. 
       // Especially, if implementation of GenericFunction is in another library
       // with not source code available to even see its implementation 
       // -- just headers
    }    
    
    void SomeOtherFunctionProbablyInAnotherUnitOfCompilation()
    {
       Derived2 d2;
       GenericFunction(&d2); // oops!
    }
    

    You have to check if dynamic_cast is actually successful. There are two ways of doing it: checking it before and after the cast. Before the cast you can check if the pointer you’re trying to cast is actually the one you expect via RTTI:

    if (typeid(b) == typeid(Derived1*))
    {
       // in this case it's safe to call the function right 
       // away without additional checks
       dynamic_cast<Derived1*>(b)->SpecialD1DoSomething();
    }
    else
    {
      // do something else, like try to cast to Derived2 and then call
      // Derived2::SpecialD2DoSomething() in a similar fashion
    }
    

    Checking it post-factum is actually a bit simpler:

    Derived1* d1 = dynamic_cast<Derived1*>(b);
    if (d1 != NULL)
    {
       d1->SpecialD1DoSomething();
    }
    

    I’d also say it’s a bad practice to try and save typing while programming in C++. There are many features in C++ than seem to be completely fine to be typed shorter (i.e. makes you feel ‘that NULL will never happen here’), but turn out to be a pain in the ass to debug afterwards. 😉

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

Sidebar

Related Questions

Suppose I have two classes a base class and an inherited class as follows:
Suppose you have a collection of Foo classes: class Foo { public string Bar;
suppose I have a domain classes: public class Country { string name; IList<Region> regions;
Suppose I have classes Foo and Bar as follow: public class Foo { public
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
Suppose I have two classes like so: class Parent def say I am a
suppose I have two classes class A extends class B class A has its
Suppose I have three classes. It is valid to instantiate A, but there are
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new

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.