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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:04:54+00:00 2026-05-23T22:04:54+00:00

If my class has a pointer of some sort that can be set by

  • 0

If my class has a pointer of some sort that can be set by the class clients, how should I deal with deletion?

Example:

class A {
};

class B {
public:
   void setA(A* a) {
       this->a = a;
   }
private:
   A* a;
};

How should be the destructor of the class B? Should it delete a? As I see, there are two way a user can set this pointer:

... // Assume B object b being created elsewhere
A aObj;
A* aPtr = new A();

b.setA(&aObj); // It is not OK to use delete, and class member will
               // point to invalid memory location once aObj goes out
               // of scope
b.setA(aPtr);  // Using new will make the pointer available even after
               // this block of code
...

So what is the right way of deleting b? Should I always perform a new in my set method?

  • 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-23T22:04:55+00:00Added an answer on May 23, 2026 at 10:04 pm

    How should be the destructor of the class B? Should it delete a?

    You, the author of the class, decides of its semantics. Don’t think in terms of pointers, references, and deletes. Think in terms of design: what’s the relation between A and B? What does B needs a A for?

    Two common types of relation are delegation and composition. Delegation would mean that client code uses the setA member to have the instance aware of some other B instance that it may use for further uses. Composition would mean that the setA member is used to initialize an internal part of the instance.

    One possible implementation of delegation is using a member pointer. I’d recommend passing a reference to setA to assign to that pointer; it sidesteps the issue of checking for 0 and makes it obvious to client code that there is no ownership issue to deal with. This is compatible with polymorphic types.

    One possible implementation of composition is using a A member, and passing by reference to const or by value to assign to it. Another is to use a smart pointer, especially if A is meant to be used polymorphically. Passing by smart pointer is the simplest thing to do — but you’ll have to check for 0 and/or document the cast.

    No matter what you decide to use (which doesn’t have to be in this list anyway), use code as a tool to achieve your purpose or design. Don’t let code dictate your thoughts.

    In all my example implementations you don’t have to do anything special in the destructor.

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

Sidebar

Related Questions

Some class has ugly field called URL, but Id rather call it file. public
FxCop has the CollectionPropertiesShouldBeReadOnly rule that complains if your class has some kind of
I'm wanting a non-reference counted smart pointer that can combine some of the useful
I'm wanting to test that a class has an EventHandler assigned to an event.
How can i check to see if a static class has been declared? ex
I have a class Application that my global.asax inherits from. The class has this
You can pass a function pointer, function object (or boost lambda) to std::sort to
I've got a property contained in a class for example public class Greeter {
A class has a property (and instance var) of type NSMutableArray with synthesized accessors
Is it possible to know whether a Java class has been loaded, without attempting

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.