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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:14:20+00:00 2026-05-28T02:14:20+00:00

I need a collection in which i can store heap-allocated objects having virtual functions.

  • 0

I need a collection in which i can store heap-allocated objects having virtual functions.

I known about boost::shared_ptr, std::unique_ptr (C++11) and boost::ptr_(vector|list|map), but they doesn’t solve duplicate pointer problem.

Just to describe a problem – i have a function which accepts heap-allocated pointer and stores it for future use:

void SomeClass::add(T* ptr)
{
  _list.push_back(ptr);
}

But if i call add twice with same parameter ptr – _list will contain two pointers to same object and when _list is destructed multiple deletion of same object will occur.

If _list will count pointer which he stores and uses them at deletion time then this problem will be solved and objects will not be deleted multiple times.

So the question is:

Does somebody knows some library with collections (vector,list,map in essence) of pointer with auto-delete on destruction and support of reference counting?

Or maybe i can solve this problem using some other technique?

Update:

I need support of duplicate pointers. So i can’t use std::set.

As Kerrek SB and Grizzly mentioned – it is a bad idea to use raw pointers in general and suggests to use std::make_shared and forget about instantiation via new. But this is responsibility of client-side code – not the class which i designs. Even if i change add signature (and _list container of course) to

void SomeClass::add(std::shared_ptr<T> ptr)
{
   _list.push_back(ptr);
}

then somebody (who doesn’t know about std::make_shared) still can write this:

SomeClass instance;
T* ptr = new T();
instance.add(ptr);
instance.add(ptr);

So this is not a full solution which i wait, but useful if you write code alone.

Update 2:

As an alternative solution i found a clonning (using generated copy constructor). I mean that i can change my add function like this:

template <typename R>
void SomeClass::add(const R& ref)
{
  _list.push_back(new R(ref));
}

this will allow virtual method (R – class which extends some base class (interface)) calls and disallow duplicate pointers. But this solution has an overhead for clone.

  • 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-28T02:14:21+00:00Added an answer on May 28, 2026 at 2:14 am

    Yes: std::list<std::shared_ptr<T>>.

    The shared pointer is avaiable from <memory>, or on older platforms from <tr1/memory>, or from Boost’s <boost/shared_ptr.hpp>. You won’t need to delete anything manually, as the shared pointer takes care of this itself. You will however need to keep all your heap pointers inside a shared pointer right from the start:

    std::shared_ptr<T> p(new T);    // legacy
    auto p = std::make_shared<T>(); // better
    

    If you another shared pointer to the same object, make a copy of the shared pointer (rather than construct a new shared pointer from the underlying raw pointer): auto q = p;

    The moral here is: If you’re using naked pointers, something is wrong.

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

Sidebar

Related Questions

I need a collection of objects which can be looked up by a certain
i need to bind a collection of objects to a combo box which i
I have a large collection of CoreData objects which represent files. Periodically, I need
I need to search a name which is stored in collection. Search criteria: eg:
I need to create feature which will iterate through all subsites of site collection
I have a collection of HTML documents for which I need to parse the
I need a function, which is capable of iterating over the collection, calling a
I've got a collection (List<Rectangle>) which I need to sort left-right. That part's easy.
I am working on a project in CakePHP 1.3 which will store a collection
Can someone suggest a good module in perl which can be used to store

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.