Link to home
Start Free TrialLog in
Avatar of lar_jens
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\GlobalDirectory\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
...



ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mahno
mahno

Hi lar_jens,

..h file:
//...
public:
myClass operator = (const myClass &Arg);
//...


..cpp file:
myClass myClass::operator = (const myClass &Arg) {
  //...
  return *this;
}

Hope that helps
  mahno

Avatar of lar_jens

ASKER

Thank you.. =)
mahno. In most cases operator = can return a reference.  That will usually be better than returning a value.