2

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

3
  • please replace the name crap with something less offensive to grandmothers. Commented Mar 4, 2014 at 4:25
  • 3
    @Cheersandhth.-Alf amazon.com/Everyone-Turtleback-Library-Binding-Edition/dp/… Commented Mar 4, 2014 at 4:34
  • @Cheersandhth.-Alf yeah sorry about that, I was actually just following TheNewBoston style. Commented Mar 4, 2014 at 4:44

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.