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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:11:34+00:00 2026-05-16T07:11:34+00:00

How do we ususaly deal with a vector whose elements are pointers to object?

  • 0

How do we ususaly deal with a vector whose elements are pointers to object? My specific question is the comment at the end of the code supplied below. Thanks.

class A
{
 public:
 virtual int play() = 0 ; 
};

class B : public A 
{
public:
 int play() {cout << "play in B " << endl;};

};

class C : public A 
{
public:
 int play() {cout << "play in C " << endl;};

};


int main()
{

    vector<A *> l;
    l.push_back(new B());
    l.push_back(new C());

    for(int i = 0 ; i < l.size();i++)
    {
            l[i]->play();
    }

    //Do i have to do this to avoid memory leak? It is akward. Any better way to do this? 
    for(int i = 0 ; i < l.size();i++)
    {
            delete l[i];
    }

  }
  • 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-16T07:11:34+00:00Added an answer on May 16, 2026 at 7:11 am

    Yes, you have to do that to avoid memory leak. The better ways to do that are to make a vector of shared pointers (boost, C++TR1, C++0x, )

     std::vector<std::tr1::shared_ptr<A> > l;
    

    or vector of unique pointers (C++0x) if the objects are not actually shared between this container and something else

     std::vector<std::unique_ptr<A>> l;
    

    or use boost pointer containers

      boost::ptr_vector<A> l;
    

    PS: Don’t forget A’s virtual destructor, as per @Neil Butterworth!

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

Sidebar

Related Questions

Often I find myself in a situation where I have to deal with catching
It may be a subjective question but I want to know what people feel
I usualy found the following code: <a href=# onclick=func();return false>click</a> but sometime my browser
I always get either malloc() error. Here is the code: char *data = malloc(200);
The following code ususally works well for me when I am using .gif or
I do some get/post action in my jquery code which return some html code.
I am working with a project where I need to store a object in
I have a page that loads with content. I .clone() certain elements, then hide
I am using XCode4.2 and developing and iPhone App I ususally save data using
basically i am trying to draw the path i.e road path. for that i

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.