Solved

Overloading the = operator?

Posted on 2000-04-07
4
204 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 Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

790 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