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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:07:28+00:00 2026-06-11T21:07:28+00:00

I created a container that controls the life cycle (new/delete) of certain types of

  • 0

I created a container that controls the life cycle (new/delete) of certain types of objects to avoid any programming mistakes. For example, a object is deleted without any notification to the container. The objects inherit from the same base class (GreetingBase).

For the implementation I am using a “template trick”:

class GreetingBase{
public:
  virtual void sayHello(){
    std::cout << "Hello there!" << endl;
  }

  virtual ~GreetingBase(){}
};

class GreetingDerived: public GreetingBase{
public:
  virtual void sayHello(){
    std::cout << "Hola amigos!" << endl;
  }
  virtual ~GreetingDerived(){}
};

class GreetingContainer{
public:
   template <class T>
   void addClass(){
      items.push_back(new T());
   }
~GreetingContainer(){
   for(std::vector<GreetingBase*>::iterator it = items.begin();
   it < items.end(); it++ ){
   delete *it;
 }
}

void talk(){
  for(std::vector<GreetingBase*>::iterator it = items.begin();
    it < items.end(); it++ ){
      (*it)->sayHello();
   }
  }
private:
 std::vector<GreetingBase*> items;
};


int main(){
  GreetingContainer container;
  container.addClass<GreetingDerived>();
  container.talk();
  return 0;
}

Questions:

  1. Using templates in order to solve this problem is a common approach? any drawbacks?
  2. Any “standard way” to report better errors messages when “T” is not derived from “GreetingBase”

Thank you in advance.

  • 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-11T21:07:29+00:00Added an answer on June 11, 2026 at 9:07 pm

    Using templates in order to solve this problem is a common approach?

    I don’t know whether it’s common, but it looks sensible enough. It ensures that your container can only contain pointers to objects allocated with new, which is good.

    any drawbacks?

    The main problem is that your container breaks the Rule of Three. It has an implicitly generated copy constructor and copy-assignment operator which simply copy each pointer; that will give you two container objects whose destructors both try to delete the same objects.

    The easiest way to fix that is by deleting these member functions, or declaring them private (with no implementation) if you’re stuck with a pre-2011 version of the language. If you need to be able to copy the container, then you’ll need to implement them to do so safely.

    Personally, I’d use smart pointers rather than rolling my own RAII container; std::unique_ptr if the container is to have exclusive ownership, std::shared_ptr if you want to share ownership, or perhaps std::weak_ptr to hold non-owning references to objects managed elsewhere by shared pointers. If you’re stuck in the past, then unique_ptr won’t be available, but Boost provides shared_ptr, weak_ptr, and also Pointer containers similar to yours.

    Any “standard way” to report better errors messages when “T” is not derived from “GreetingBase”

    In C++11, you could perhaps use a static assert, something like:

    static_assert(std::is_base_of<GreetingBase, T>::value, 
                  "Wrong type for GreetingContainer");
    

    Alternatively, you might get a slightly more readable error message by creating a local pointer; then the error message at least won’t contain the full name of push_back:

    GreetingBase * p = new T();
    items.push_back(p);
    

    The error message will be something like can't convert Whatever* to GreetingBase*, which should be clear enough.

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

Sidebar

Related Questions

I always seem to struggle with asp.net page cycle and dynamically created controls. I
I am new to WPF. I have Created some user Controls with some basic
I have created a form that contains fields used to accept a floating value
I've created an app that contains 4 viewcontroller and its .h,.m files...In my firstviewcontroller
I created a graphic in Flash CS4 that contains text. I embedded the appropriate
I've created a class library that contains three entity models for three databases that
i´ve just created an iOS app that contains lots of media. Now i just
I have a listview that contain several EditTexts created dynamically according to the number
I have a C# 2010 application that contains a report created using Crystal Reports
I have a table that contains id, created, user_id. I'm wondering if there is

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.