Solved

Overloading the = operator?

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now