I understand the difference between the copy ctor & the assignment operator and when each is used
I'm hoping someone can clear up a few things for me. I'm sure I've been to any of the major websites you might direct me to explaining these things, and I'm sure you're tired of reiterating this discussion, but pleez someone explain some of the following things that I'll comment in the code:Code:SomeObject object2(object1); // This calls the copy ctor SomeObject object2 = object1; // This calls the assignment operator
Your simplest explanation breaking this down is much appreciated.Code:class MyClass{ public: MyClass(const MyClass& rhs) : m_someData(rhs.m_someData), m_my ClassPtr(rhs.m_myClassPtr) {}; // Is this the correct copy ctor? MyClass& operator=(const MyClass& rhs){ if ( this != &rhs ){ // What exactly does this line do? m_someData = rhs.m_someData; m_myClassPtr = rhs.m_myClassPtr; // Is this correct? } return *this; }; // Is my assignment operator correct or horribly misguided? private: int m_someData; MyClass* m_myClassPtr; };