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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:48:44+00:00 2026-05-11T02:48:44+00:00

Just wanted opinions on a design question. If you have a C++ class than

  • 0

Just wanted opinions on a design question. If you have a C++ class than owns other objects, would you use smart pointers to achieve this?

class Example { public:    // ...  private:   boost::scoped_ptr<Owned> data; }; 

The ‘Owned’ object can’t be stored by value because it may change through the lifetime of the object.

My view of it is that on the one side, you make it clear that the object is owned and ensure its deletion, but on the flipside, you could easily just have a regular pointer and delete it in the destructor. Is this overkill?

Follow up: Just wanted to say thanks for all your answers. Thanks for the heads-up about auto_ptr leaving the other object with a NULL pointer when the whole object is copied, I have used auto_ptr extensively but had not thought of that yet. I make basically all my classes boost::noncopyable unless I have a good reason, so there’s nothing to worry about there. And thanks also for the information on memory leaks in exceptions, that’s good to know too. I try not to write things which could cause exceptions in the constructor anyway – there are better ways of doing that – so that shouldn’t be a problem.

I just had another question though. What I wanted when I asked this question was to know whether anyone actually did this, and you all seem to mention that it’s a good idea theoretically, but no one’s said they actually do it. Which surprises me! Certainly one object owning a pointer to another is not a new idea, I would have expected you all would have done it before at some point. What’s going on?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T02:48:45+00:00Added an answer on May 11, 2026 at 2:48 am

    It’s a good idea. It helps simplify your code, and ensure that when you do change the Owned object during the lifetime of the object, the previous one gets destroyed properly.

    You have to remember that scoped_ptr is noncopyable, though, which makes your class noncopyable by default until/unless you add your own copy constructor, etc. (Of course, using the default copy constructor in the case of raw pointers would be a no-no too!)

    If your class has more than one pointer field, then use of scoped_ptr actually improves exception safety in one case:

    class C {   Owned * o1;   Owned * o2;  public:   C() : o1(new Owned), o2(new Owned) {}   ~C() { delete o1; delete o2;} }; 

    Now, imagine that during construction of a C the second ‘new Owned’ throws an exception (out-of-memory, for example). o1 will be leaked, because C::~C() (the destructor) won’t get called, because the object has not been completely constructed yet. The destructor of any completely constructed member field does get called though. So, using a scoped_ptr instead of a plain pointer will allow o1 to be properly destroyed.

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

Sidebar

Related Questions

I'm new to database design and just wanted a few opinions as to whether
just wanted to ask where I define initial class properties? From other languages I
Just wanted some opinions on the matter. I have always used int flags, and
I just wanted some opinions from people that have run Selenium ( http://selenium.openqa.org )
Just wanted your expert opinions on declarative transaction management for Spring. Here is my
Just wanted to know how i would replace sitename.com with sitename2.com whenever a user
Just wanted to know if anyone is really using Objects and Collections in Oracle
Just wanted to get a few opinions really, I'm trying to increase the loading
I just wanted to hear your Opinion about a WCF Client implementation. I have
I've just come across a pattern I've seen before, and wanted to get opinions

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.