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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:38:28+00:00 2026-05-27T22:38:28+00:00

I have a base class B and several derived template classes D<int> , D<float>

  • 0

I have a base class B and several derived template classes D<int>, D<float>, D<double>, etc. (so more than ten)

In my program, I find a situation where I have a B pointer that I KNOW points to an instance one of the D specializations. I also have a unique key that identifies the derived type.

So, I want to call the correct derived class method using my base class pointer and the unique type key. I actually want to do this in several places, so the one solution I have come up with is not only ugly, but cumbersome.

Is there a better way to access the derived class’s members having a base pointer and a unique key in C++?

I don’t want to / can’t modify the base class. boost library is fair game.

Here is my potential code, but I’m not happy with replicating this everywhere I need to access a derived member function/variable. All of this for one member function call?!!

B * bptr = ...
std::type_info * typekey = ...

if        (typekey == &typeid(D<float>) ) {
    D<float> * dptr = static_cast<D<float>*>(bptr);
    dptr->derivedMember();
} else if (typekey == &typeid(D<double>) ) {
    D<float> * dptr = static_cast<D<double>*>(bptr);
    dptr->derivedMember();
} else if (typekey == &typeid(D<int>) ) {
    D<float> * dptr = static_cast<D<int>*>(bptr);
    dptr->derivedMember();
} 
  • 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-27T22:38:28+00:00Added an answer on May 27, 2026 at 10:38 pm

    If all the D<type>:: methods have the same name ‘DerivedMember`, and I assume that the base class B does not declare this as virtual, can you change your hierachy from:

    class B { etc. };
    
    template<class T>
    class D : public B
    {
    };
    

    to:

    class B { etc. };
    
    class C : public B
    {
     virtual void derivedMember() = 0;
    };
    
    
    template<class T>
    class D : public C
    {
    public:
    void derivedMember() { etc. };
    };
    

    Then you could have:

    void f(C* c) /* or take a B* and do: C* c = static_cast<C*>(b); */
    {    
      c->derivedMember();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a Base class and several Derived classes. Is there any
I have a base class and several derived classes. The derived classes use some
I have a base class with several derived classes that extend it. I want
I have a base class where I derive several classes. I have another class
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have a base class vehicle and some children classes like car, motorbike etc..
class base { int a; protected: template<class T> class derived; public: base() {} virtual
I have a base class and several subclasses derived from that base class. I
I have a Base Class, called primitive Graphics. derived from this class are several
I have a common assembly/project that has an abstract base class, then several 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.