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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:21:16+00:00 2026-06-11T10:21:16+00:00

I have created a smart pointer class like: template <class T> class Owner {

  • 0

I have created a smart pointer class like:

template <class T>
class Owner
{
    T* m_p;
public:
    Owner(T *p=0) : m_p(p) {}
    ~Owner() { if (m_p) delete m_p; }
    T* operator ->() { return m_p; }
    T& operator *() { return *m_p; }
    // other members.
};

It works well (resembles to the auto_ptr in boost library), but now I have the requirements that I want to store an dynamic array of smart pointers in an obj, and it must support:

1) insert new smart pointer into the smart pointers array to let the array resize and acquire the ownership of the new obj,

2) delete one smart pointer on the fly and the resource get freed,

3) when finalizing the array, all objects get deleted.

I was thinking using std::vector<Owner<T> >, but seems c++ best practice suggests not storing smart pointers in std containers, because copy/assignment behaviors, so what other things can I employ to implement this? something like the OwnerArr in the below example:

 class Adapter;

 class Computer
 { 
 public:
      Computer() {}
      ~Computer() { // adapters get freed automatically here. }

      void insertAdapter(Adapter* pAdapter) { m_adapters->appendOne(pAdapter); }
      OwnerArr<Adapter>   m_adapters;
 };

Thanks 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-11T10:21:18+00:00Added an answer on June 11, 2026 at 10:21 am

    You can’t store your Owner, or std::auto_ptr (which you shouldn’t use anyway, since it’s deprecated), in a standard container because they are not really copyable.

    In C++11, there’s std::unique_ptr: a single-ownership smart pointer to replace auto_ptr, which is movable but not copyable. This can be moved or emplaced into any container, as long as you don’t need to do anything that involves copying it. If you need multiple pointers to share ownership of the same object, then you can use std::shared_ptr instead.

    If you’re stuck with an older language version for some reason, then Boost can give you smart pointers, including a shared_ptr very similar to the standard one, or pointer containers similar to your OwnerArray.

    If you do decide to roll your own resource management classes, always remember the Rule of Three. The classes you show have implicitly generated copy constructors and copy-assignment operators which can cause two pointers to own – and try to delete – the same object, which is very bad. You need to either prevent copying (by deleting these functions, or (pre-2011) declaring them private with no implementation), or implement some kind of safe copy semantics.

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

Sidebar

Related Questions

I have created a smart pointer implementation as given below :: #include <iostream> #include
I have a class with a (non smart) pointer to an interface object (lets
I have class foo that contains a std::auto_ptr member that I would like to
I have a list of smart pointers where each pointer points to a separate
I have a class that creates an object inside one public method. The object
I have created an application.in the app i start a countdown timer and save
I have created a C# Windows service but it fails to start. I get
I have a created a panel. This has autoscroll = true At the start
I have an application and every new created activity will start an async task
I have created a QWidget with a bunch of QToolButtons in it and 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.