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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:32:29+00:00 2026-06-13T08:32:29+00:00

I am learning pointers in c++ and am having some trouble. I have a

  • 0

I am learning pointers in c++ and am having some trouble.
I have a class Foo that in the header file declares some data:

private:
const Bar *obj;

Where Bar is a class.

Then in the c++ implementation I want to replace *obj so that it points to a completely different Bar object. *obj is constant, so how do I change what is in what *obj points to or rather, what is in memory at *obj? Also in Foo‘s destructor, how do I deallocate *obj?

  • 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-13T08:32:30+00:00Added an answer on June 13, 2026 at 8:32 am

    Given your class definition

    class A {
    private:
      const Bar *obj;
    };
    

    obj is a pointer to a constant Bar object. You can change what that pointer points to, but you cannot change the contents of the object pointed to.

    So, if you have a new Bar object and you’d like to change obj so it points to that, you can simply assign the new value:

    /* New object: */
    Bar *new_bar = new Bar;
    /* Change the pointer: */
    obj = new_bar;
    

    There are two issues, however.

    1. If the new Bar object is created outside the class, you cannot directly assign it to obj because the latter is private. Hence you need a setter function:

      class A {
      private:
        const Bar *obj;
      public:
        void set_obj(const Bar *new_obj) { obj = new_obj; }
      };
      
    2. You must determine who will eventually own the Bar object, i.e. who is responsible for freeing the heap space it takes. If the caller is responsible then you can code it as above, i.e. class A will never create new Bar objects, nor delete any. It will just maintain a pointer to Bar objects created and deleted outside the class.

      But if class A above is actually responsible for the memory space taken by the Bar objects, you must use delete obj in the destructor to free the space, and you must also free the space when you get a new Bar object assigned. That is, the set_obj function above needs to be changed to this:

      void set_obj(const Bar *new_obj) { delete obj; obj = new_obj; }
      

      Otherwise you’ll have a memory leak. Similar measures must be taken in the copy constructor (unless you delete it), as well as the assignment operator: Both functions are used whenever a copy of a class A object is made, and in that case you must make sure that you do not simply copy the pointer, but instead allocate fresh space and copy the object (i.e. you must perform a deep copy):

      A(const A& a):obj(new Bar(*a.obj)) {}
      A& operator=(const A& a) { delete obj; obj = new Bar(*a.obj); return *this; }
      

      Having said this, if your class is responsible for the memory space, it is a much better idea to use a smart pointer class instead of a raw pointer. The main reasons are: (i) The above is quite complicated and it’s easy to make mistakes; (ii) The above is still not very good – there may still be memory leaks or worse problems when an exception is thrown, e.g. in the constructor of Bar. C++11 provides a smart pointer class called std::unique_ptr, which seems ideal for your purposes:

      class A {
      private:
        std::unique_ptr<const Bar> obj;
      public:
        ~A() {}
        void set_obj(std::unique_ptr<const Bar> new_obj) { obj = new_obj; }
      };
      

      With this in place, the smart pointer will take care of any memory space that needs to be freed automatically, both at destruction time as well as when a new Bar object is assigned to the pointer.

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

Sidebar

Related Questions

I am learning about pointers and arrays in class. My trouble is in the
I'm learning pointers and references but I'm having trouble grasping the concept. I need
I am very new to C and I have some problems learning about pointers.
I have a class called Foo which has a member that is a pointer
I'm learning about pointers. Given this code: FILE* from = fopen("in.txt", "r"); FILE* to
Need some pointers to resources for learning about Image processing used in Augmented Reality.
I am learning c++, and I am having issues doing some newbie things. I
I'm having a few problems with some Objective-C and would appreciate some pointers. So
I'm learning C and now I'm having confusion in pointers. My question is, why
I am learning about pointers and the new operator in class. In my readArray

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.