Question about Copy Constructor and Overloading Assignment Operator
Hi everbody,
I'm self-studying C++, so there are some points confusing me. Please help me make it clear:
Consider this Copy Constructor Declaration:
MyClass(const MyClass& myObj);
Why must a copy constructor have call-by-reference parameter ? what happens if that is call-by-value parameter ? If call-by-value, will the compiler assign the parameter to another copy of the argument, and their pointers (pArr) will be pointing to the same array in memory that will later be destroyed by the destructor, when the temporary myObj goes out of scope once Copy Constructor finishes its job ?
Code:
MyClass
{
public:
/* .... */
MyClass(const MyClass& myObj);
~MyClass();
operator=(const Myclass& rsideObj);
/* ... */
private:
/* ... */
char *pArr; /* used to create dynamic array type of char (a C_string, I mean ... */
};
Why operator=(const MyClass& rsideObj) must be a member function, rather than friend function, just like other overloading operators?