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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:21:30+00:00 2026-05-17T17:21:30+00:00

When I have a class that contains pointers as member variables what type of

  • 0

When I have a class that contains pointers as member variables what type of smart pointer should they have if I want don’t want to use plain pointers? They do not need to be shared (so no shared_ptr necessary). scoped_ptr won’t work since I often need to build the objects outside of the initialization list.

Or is it maybe common practice to use a scoped_ptr during the creation when something can still fail (exceptions thrown etc.) and afterwards assign them to plain pointers?

  • 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-17T17:21:30+00:00Added an answer on May 17, 2026 at 5:21 pm

    If you’re just wanting to store member pointers in a smart pointer type class so you can’t/won’t forget to delete them, then a standard choice would be auto_ptr. It’s in the STL and is easily “reset” with the reset() function when you need to release the current memory allocated to it and replace it with a new object.

    You will still want to implement your own copy constructor and assignment operators for the classes which have auto_ptr members. This is due to the fact that auto_ptrs assignment operator transfers ownership of the underlying object so a default assignment operator will not have the effect you want.

    Here is what the class might look like:

    class X
    {
    public:
        X() :p(new ClassToManage) {}
        X(const X &copy)
            :p(new ClassToManage(*copy.p))
        {
        }
    
        X &operator=(const X &rhs)
        { 
            this->p.reset(new ClassToManage(*rhs.p));   
        }   
    
    private:
        std::auto_ptr<ClassToManage> p;
    };
    

    For all other cases I would suggest boost::shared_ptr. Shared_ptr does do reference counting but you can store them in standard containers which makes them quite useful.

    You should ultimately try to rid yourself of using plain pointers for anything which points at allocated memory it’s responsible for deleting. If you want to use a plain pointer for accessing or iterating over a plain ole array etc., then that’s fine (but ask yourself why you’re not using a std::vector), but when you use them to point at something that it is responsible for freeing then you’re asking for trouble. My goal when writing code is to have no explicit deletes.

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

Sidebar

Related Questions

I have a class that contains an array. I want this array to be
I have a vector-like class that contains an array of objects of type T
I have a class that I want to use to store properties for another
I have class foo that contains a std::auto_ptr member that I would like to
I have a class that I've been provided that I really don't want to
I have a class that contains a dynamically allocated array, say class A {
I have a class that contains two methods like these: public String getFoo(Int32 a)
I have a class that contains a list of objects. What's the best way
I have a class file that contains all the classes that are needed for
I have a class that I need to binary serialize. The class contains one

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.