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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:57:12+00:00 2026-06-09T23:57:12+00:00

I have Derived Classes that inherit from a Base class with virtual functions. Im

  • 0

I have Derived Classes that inherit from a Base class with virtual functions. Im using smart pointers(shared_ptr) in order to create the objects because i want the objects to be appended into a vector. But i noticed my code to be repetitive with handling to objects to do certain tasks so i thought a template could be solution to improve my code.
This is my attempt so far(not the exact code, simplified):

class Base{

    public:
     virtual ~Base(){}
     virtual void display_message() = 0;
};

class DerivedA : public Base{
    DerivedA(){}
};

class DerivedB : public Base{
    DerivedB(){}
};



//THE template- 
//<hold the smart pointer that points to different derived objects>

template<typename T1>

class HandleInstances{
      private:            

         vector<T1> ObjectVector;
         //the iterator

         T1 sp_base;

      public:

        HandleInstance(const T1 & sp){
            sp_base = sp; // set smart pointer
        }
        //somefunctions

        //this is what i need to figure out
        void AddToVector(){
             ObjectVector.push_back(sp_base(new 'The derived class') );
        }



};

The AddToVector functions is the problem here. in order to add an element of an object i have to do this push_back( “the smart pointer”( new “the class” ));. how do i let the template accept the class (not an object) and implement it to the function of push_back() ?

  • 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-09T23:57:14+00:00Added an answer on June 9, 2026 at 11:57 pm

    This is a challenging one – why? Because templates operate as a sort of preprocessing step in C++ by dynamically constructing variants of classes based on their template parameter.

    In other words, shared_ptr<DerivedA> and shared_ptr<DerivedB> have no relationship to each other whatsoever, because in C++, templates essentially make them like 2 separate class declarations – unrelated types.

    The pointers they contain are both descendants of the base class, but the shared_ptrs themselves might as well be a Vector<Bool> and a Foo. It doesn’t matter that DerivedA and DerivedB inherit from a single base class, their template-generated classes do not.

    That being said, you do have some flexibility: if you put a factory function in your classes’ interface, like this:

    class DerivedA : public Base{
        public:
        static shared_ptr<DerivedA> construct{
            return shared_ptr<DerivedA>(new DerivedA());
        };
    };
    
    ....similar for DerivedB
    

    then you can do something like this:

    template<typename T>
    class HandleInstances{
      private:            
    
         vector<shared_ptr<T> > ObjectVector;
    
      public:
    
        //this is what i need to figure out
        void AddToVector(){
             ObjectVector.push_back(T::construct());
        }
    };
    

    and you should be OK. What you’re doing is giving each class a function to make up for the fact that the class itself can’t be stored to make objects later. Notice:

    1. The template parameter in HandleInstances is the base type, not a shared_ptr type.
    2. Construct is returning different types in DerivedA and DerivedB.
    3. Base has no declaration of construct – because these functions do not return compatible types, they are not inherited from Base.
    4. You still can’t mix and match types in your ObjectVector, if that’s what you were hoping – but you were never going to be able to do that anyway. (Bonus: it is possible if you use more types to wrap your types and handle this: see boost::any
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Derived that inherits directly from two base classes, Base1 and
I have a base class that inherits List<> which has multiple classes derived from
I have multiple classes that inherit from one base class, and each class is
I have two Parser classes that inherit from a base class, BaseParser. I want
I have a virtual class Element that forces its derived classes to have a
I have a base class, SpecialClass . Lots of other classes inherit from it,
I have a base class with several derived classes that extend it. I want
I have multiple classes that all derive from a base class, now some of
I have class Base and classes Derived_1 , Derived_2 ... I need derived classes
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC

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.