Solved

Overloading the = operator?

Posted on 2000-04-07
4
205 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 50 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

Industry Leaders: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

697 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