Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Overloading the = operator?

Posted on 2000-04-07
4
Medium Priority
?
208 Views
Last Modified: 2010-04-02
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
...



0
Comment
Question by:lar_jens
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 7

Accepted Solution

by:
KangaRoo earned 200 total points
ID: 2692569
class myClass {
  private:
     CString m_FirstName;
     CString m_LastName;

  public:
  // getter and setter methods..
  // blablabla.. the usual..

  myClass& operator=(const myClass& other);
}

myClass& myClass::operator=(const myClass& other)
{
    m_FirstName = other.m_FirstName;
    m_LastName = other.m_LastName;
    return *this;
}
0
 
LVL 1

Expert Comment

by:mahno
ID: 2692581
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

0
 
LVL 1

Author Comment

by:lar_jens
ID: 2692591
Thank you.. =)
0
 
LVL 22

Expert Comment

by:nietod
ID: 2693052
mahno. In most cases operator = can return a reference.  That will usually be better than returning a value.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question