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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:25:49+00:00 2026-06-07T15:25:49+00:00

I have three classes organized in following manner. Foo is a template class, Bar

  • 0

I have three classes organized in following manner. Foo is a template class, Bar derived from Foo, and Doo derived from Foo too. All of them implement a doX() member function which is defined in Foo as a virtual function.

I need to have a vector (or any other container) of Bar and Doo objects. For example, a vector of two objects named vec, first element should be a Doo and second should be a Bar. When I call vec[0].doX() Doo::doX() should be called. Defining the vector of pointers to Foo objects, do the job. But I’m not sure where do the instances actually stored. If I put pointers to objects, the allocated memory may be released after leaving the scope that objects are created in.

I prepared a minimal working example for illustrating the problem:

#include <iostream>
#include <vector>

using namespace std;

template <typename T>
class Foo
{
public:
    virtual void doX(){cout<<"doX from Foo"<<endl;}
};

template <typename T>
class Bar : public Foo<T>
{
public:
    void doX(){cout<<"doX from Bar"<<endl;}
    void g(string const &input);    // some test function may be used in doX
    int x;                          // some test data may be used in doX
};

template <typename T>
class Doo : public Foo<T>
{
public:
    void doX(){cout<<"doX from Doo"<<endl;}
};

void doSomething(vector<Foo<int>* >& target)
{
    Foo<int> X;
    // do some extreme job to generate X
    target.push_back(&X); // ==> This is problematic
}

int main()
{
    Foo<int> f;
    Bar<int> b;
    Doo<int> d;
    vector<Foo<int> > v;
    v.push_back(f);
    v.push_back(b);
    v.push_back(d);
    v[0].doX();             // doX from Foo
    v[1].doX();             // doX from Foo
    v[2].doX();             // doX from Foo
    vector<Foo<int>*> v2;
    v2.push_back(&f);       // here is no problem, but if `f` is generated inside
                            // a function that receives a reference to `vec` there
                            // will be problems
    doSomething(v2);        // This is problematic
    v2.push_back(&b);
    v2.push_back(&d);
    v2[0]->doX();           // doX from Foo
    v2[1]->doX();           // doX from Foo but dangerous! May crash
    v2[2]->doX();           // doX from Bar
    v2[3]->doX();           // doX from Doo

    return 0;
}
  • 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-07T15:25:52+00:00Added an answer on June 7, 2026 at 3:25 pm

    You have correctly identified the problem. You have to make sure that the instances that are pointed to by the pointers in your vector live at least as long as the vector itself. You can store pointers to dynamically allocated objects, which means that you are in charge of controlling their lifetime. This can be achieved by storing pointers to instances created with new, or better still, smart pointers thereof.

    void addElements(vector<Foo<int>* >& target)
    {
        target.push_back(new Bar<int>());
        target.push_back(new Doo<int>());
    }
    

    In the above example, you have to make sure to delete the elements of the vector when you are done (typically before the vector goes out of scope).

    A C++11 example with std::unique_ptrs:

    std::vector<std::unique_ptr<Foo<int>> v;
    v.push_back(std::unique_ptr<Foo<int>>(new Bar<int>()); // moves the unique_ptr
    v.push_back(std::unique_ptr<Foo<int>>(new Doo<int>()); // moves the unique_ptr
    v.emplace_back(new Bar<int>()); // constructs the unique_ptr in place
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three classes: Base, Derived (inherits from Base), and Stats (which uses a
I have three classes that all have a static function called 'create'. I would
I have three classes; Classes A and B both reference class C . How
If I have three classes class A class B extends A class C extends
I have three classes: public class A { public A(){ System.out.println(in A); } }
I have three classes which all make different works but I need to run
Have three classes User, Group and Field. Many to many relationship on User /
I have three classes that I am using and have shortened for ease of
I have three classes: PurchaseOrder, PurchaseOrderLine, Item PurchaseOrder children are PurchaseOrderLine which is related
I have three classes, I am simply display my Facebook friends in tableView. And

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.