Reputation: 171
I have recently been learning overloaded operator and soon came to overloaded copy operator. I tried some example but couldn't really understand the format and how it function. Well, it would be helpful if you could explain me the code in easier terms because since I'm a beginner in c++. Anyway here is my code:
#include <iostream>
using namespace std;
class Point{
private:
int* lobster;
public:
Point(const int somelobster){
lobster = new int;
*lobster = somelobster;
}
//deep copy
Point(const Point& cpy){ //copy of somelobster(just to make sure that it does not do shallow copy)
lobster = new int;
*lobster = *cpy.lobster;
}
//assingment operator
Point& operator=(const Point& cpy){ //used to copy value
lobster = new int; //same thing as the original overloaded constructor
*lobster = *cpy.lobster; //making sure that it does not makes a shallow copy when assigning value
return *this;
}
void display_ma_lobster(){ //display nunber
cout << "The number you stored is: " << *lobster << endl;
}
~Point(){ //deallocating memory
cout << "Deallocating memory" << endl;
delete lobster;
}
};
int main(){
Point pnt(125);
cout << "pnt: ";
pnt.display_ma_lobster();
cout << "assigning pnt value to tnp" << endl;
Point tnp(225);
tnp = pnt;
tnp.display_ma_lobster();
return 0;
}
but the main part which really need explanation are:
//deep copy
Point(const Point& cpy){ //copy of somelobster(just to make sure that it does not do shallow copy)
lobster = new int;
*lobster = *cpy.lobster;
}
//assingment operator
Point& operator=(const Point& cpy){ //used to copy value
lobster = new int; //same thing as the original overloaded constructor
*lobster = *cpy.lobster; //making sure that it does not makes a shallow copy when assigning value
return *this;
}
thank you for your time
Upvotes: 2
Views: 145
Reputation: 912
A good read on this topic is: What is the copy-and-swap idiom?
Your object contains allocated memory, so each time you copy your object, you also need to perform a new memory allocation, and copy the contents of your allocation. That is why you need those things in the copy constructor. The copy constructor is automatically called when the compiler tries to copy your object, such as across a function call, of if it is placed into a container.
The assignment operator is flawed, because it needs to do a bit more or a bit less. It is used in
tnp = pnt;
Because it is handling assigning one object to another, it needs to either use the existing allocated memory, or handle the deallocation of memory in the old tnp object before the pnt memory is copied in.
Upvotes: 1
Reputation: 137770
The copy assignment operator may use existing resources already owned by the object. It is not like a constructor. (Your copy constructor is correct.)
//assingment operator
Point& operator=(const Point& cpy){ //used to copy value
// Do not allocate anything here.
// If you were to allocate something, that would require deallocating yer shit too.
*crap = *cpy.crap; //making sure that it does not makes a shallow copy when assigning value
return *this;
}
Upvotes: 2