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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:34:07+00:00 2026-06-01T04:34:07+00:00

In C++, whose responsibility is it to delete members of a class: the class,

  • 0

In C++, whose responsibility is it to delete members of a class: the class, or the creator of an instance of that class?
For example, in the following code:

class B {
  public:
    B(int x) { num = x; }
    int num;
};

class A {
  public:
    A(B* o) { obj = o; }
    B* obj;
};

int main(void) {
  A myA(new B(3));
  return 0;
}

Should main delete the instance of B, or should A‘s destructor delete its local variable obj? Is this true in most cases, and in which cases if any is it not?

  • 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-01T04:34:08+00:00Added an answer on June 1, 2026 at 4:34 am

    This is a basic question of ownership. If every A should own a B (as in, there should be a new B created when the A is created, that should also be destroyed be the A is destroyed, then you’d normally make A responsible for creating and destroying the instance of B:

    class B { 
        int num;
    public:
        B(int x) : num(x) {}
    };
    
    class A { 
        B *obj;
    public:
        A(int value) : obj(new B(value)) {}
        ~A() { delete B; }
    };
    

    In such a case, however, chances are pretty good that A should just be written like:

    class A {
        B obj;
    public:
        A(int v) : obj(v) {}
    };
    
    int main() { 
        A a(3);
        return 0;
    }
    

    This way, the instance of B will be created and destroyed automatically, without any intervention on your part at all.

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

Sidebar

Related Questions

Suppose a class whose responsibility is to set parameters to a JDBC query. It
i have a class whose equality is based on 2 fields such that if
Inside a Webservice method , i have created a helper class whose responsibility is
I have a TextBox whose value is passed to an int property in an
I have an Activity whose layout is in the file main.xml that contains a
I'm working on application whose main responsibility is to contact remote server and display
I have class MasterData whose instances contain some general information about all possible gadgets.
I have a class whose new method has been made private because I want
I have xml file whose structure is defined with following xsd: <?xml version=1.0 encoding=utf-8?>
I have a button whose ClickMode is initially set to Press. The code for

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.