A lot of the tutorial sites that I've seen have copy constructors and = operators that contain much the same code. My question, therefore, is the following valid?
This seems to be the easiest way to do a copy constructor, as are these two examples below not functionally equivalent:Code:SomeClass::SomeClass(const SomeClass &sc) { *this = sc; // After all, we're copying sc, and what is copying but an assignment? } SomeClass::operator = (const SomeClass &sc) { if(this == &sc) return *this; // Do copy, freeing current data items as needed. return *this; }
Just wondering if what I'm writing is legal. I've used the above (calling operator = in the copy constructor) and never ran into any problems... just want to know if it's waiting to blow up in my face or not.Code:// SomeClass scA has been previously defined / initialzed with data. SomeClass scB(scA); SomeClass scC = scA; SomeClass scD; scD = scA; // Are not all those functionally equivalent? // (Only differing between when the data is set to a value?)
Thanks.