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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:52:52+00:00 2026-05-30T16:52:52+00:00

So I have the standard C++ setup with an object that stores another object.

  • 0

So I have the standard C++ setup with an object that stores another object. The stored object is owned completely, it’s never leaked to the outside. The member is non-const.

class Container
{
private:
    Contained item;
}

As I understand, when my Container is instantiated the default constructor will be called on the item member and I don’t have to manage it in the initializer list.
Also do I understand it correctly that when my object is destroyed the dtor on the item will be called automatically?

Another option would be to store it by reference of course

class Container
{

private:
    Contained& item;

public:

    Container() : Contained()
    {

    }
}

in which case I don’t know whether I should delete it in the dtor.

Yet another option is to store it by ptr

class Container
{
private:
    Contained* item;
public:
    Container()
    {
        item = new Contained();
    }

    ~Container()
    {
        delete item;
    }
}

Knowing that my item never gets returned to the caller and never bleeds into the outside API and never gets reassigned, what is the best way to proceed? As I mentioned, the item member is not const
(it will be a self-resizing data structure).

  • 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-30T16:52:53+00:00Added an answer on May 30, 2026 at 4:52 pm

    The easiest is to store the object itself. Using reference for this purpose is, I would say, confusing. One advantage with using the pointer is that you then may be able to avoid defining the Contained type in the header file – you can instead forward declare Contained, and keep all the details inside the .cpp file.

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

Sidebar

Related Questions

I have standard databindings setup for all my TextBoxes to an Object like this:
I have the following setup: eclipse a standard Java project (A) an eclipse plugin
Many languages have standard repositories where people donate useful libraries that they want others
I have a standard windows server that inherits from the ServiceBase class. On the
I have created an IDbContext object that is provided to my IRepository implementations. The
I have what I assume is a fairly standard setup, with one scratchpad MOC
I have a standard Order/OrderLineItem setup. A number of Refunds are generated during the
I have a simple messaging queue setup and running using the Zend_Queue object heirarchy.
I have a standard jQuery plugin set up that creates default settings at the
We have the standard 3 environment setup of development, testing and production. Each environment

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.