C ++: making a shallow copy of an object a) syntax b) I need to delete it

I have a class MyClassA . In my constructor, I pass a pointer to an instance of class B. I have some very simple questions related to this.

(1) First, the correct code? (code that makes a shallow copy and code in methodA() )

 MyClassA::MyClassA(B *b){ this.b = b; } void MyClassA::methodA(){ int i; i = b.getFooValue(); // Should I rather be using the arrow operator here?? // i = b->getFooValue() } 

(2) I assume that I do not need to worry about deleting memory for MyClassA.b in the ~MyClassA() destructor, since it is not allocated. I'm right?

thanks

Update: Thank you all for your answers! MyclassA is only interested in accessing class B methods. It does not accept responsibility for B.

+4
source share
5 answers

<B> 1) this.b = b;

Here you pass a pointer to instance B. As a Mac, it should be:

this-> b = b;

 b.getFooValue(); 

It should be b-> getFooValue (), because MyClassA :: b is a pointer to B.

2) It depends on how you define what MyClassA :: b is. If you indicate (in the code comments) that MyClassA takes ownership of the instance B passed in the constructor of MyClassA, then you need to remove b in the destructor of MyClassA. If you indicate that it retains only the link to b, without assuming ownership, then you do not need to.

PS . Unfortunately, in your example there is no way to make ownership rights explicit , other than code documentation.

0
source
  • You need an arrow operator, since b is a pointer.
  • Yes, if the user MyClassA does not want to own b . (You can’t even be sure that b is a stack variable in which delete - if this could lead to code failure.)

Why aren’t you using a smart pointer, or is it even easier to link?

+2
source

First of all, is the following code correct? (the code that makes a shallow copy and the code in Methoda ())

The answer depends on who owns the memory responsibility of object B. If MyClassA is supposed to just keep pointer A without taking responsibility for deleting it, then this is normal. Otherwise, you need to make a deep copy.

I assume that I do not need to worry about deleting memory for MyClassA.b in the ~ MyClassA () destructor, since it is not allocated. I'm right?

Again it depends on how memory is allocated for B. Is it allocated on the stack or heap? If you do not need to explicitly free it from the stack in the MyClassA destructor, otherwise you need to delete it.

+1
source

one). It depends on the lifetime of the pointer to B. Make sure that when you call b-> getFooValue (); b must be a valid pointer.

I suggest using an initialization list, and if you are only reading the value of object B, although its pointer then turns it into a pointer to persistent data.

 MyClassA::MyClassA(const B *bObj) : b(bObj) {} 

2). While B is on the stack, you need to delete it, and if it is allocated for the heap, then it must be deleted by another owner, you will have a memory leak. You can use the smart pointer to get rid of the problem.

+1
source
 MyClassA::MyClassA(B *b){ this.b = b; 

}

it should be:

 MyClassA::MyClassA(B *b){ this->b = b; 

}

because this treated as a pointer.

+1
source

All Articles