lar_jens
asked on
Overloading the = operator?
I have a class
class myClass {
private:
CString m_FirstName;
CString m_LastName;
public:
// getter and setter methods..
// blablabla.. the usual..
}
I want to overload the = operator, but I get some funny error messages like:
E:\radec\src\GlobalDirecto ry\GLOBDir .cpp(101) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class CTAG' (or there is no acceptable conversion)
What do I do wrong?
I only want to be able to do the following:
myClass c1,c2;
...
...
c1 = c2; <- I wanna do this
...
class myClass {
private:
CString m_FirstName;
CString m_LastName;
public:
// getter and setter methods..
// blablabla.. the usual..
}
I want to overload the = operator, but I get some funny error messages like:
E:\radec\src\GlobalDirecto
What do I do wrong?
I only want to be able to do the following:
myClass c1,c2;
...
...
c1 = c2; <- I wanna do this
...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.. =)
mahno. In most cases operator = can return a reference. That will usually be better than returning a value.
..h file:
//...
public:
myClass operator = (const myClass &Arg);
//...
..cpp file:
myClass myClass::operator = (const myClass &Arg) {
//...
return *this;
}
Hope that helps
mahno