Solved

Overloading =

Posted on 1998-02-06
3
269 Views
Last Modified: 2010-04-10
I tried overloading the operator=, but it doesn't get called inside my Function(). It does not displays the
message box in the overloaded operator=. What is wrong?

// in header file
MyObject& operator= (const MyObject&);

// in cpp file
MyObject& MyObject::operator= (const MyObject& that)
{
  AfxMessageBox("Here") ;
  x = that.x;  
  return *this;
}

void MyObjec::Function()
{
  MyObject *pObj = new MyObject(1) ;
  MyObject *pTemp = pObj ;
}

leow
0
Comment
Question by:engllf
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 10 total points
ID: 1181048
That is because you are assigning pointers and not references. To use the assignment operator, you need references:

  MyObject temp;
  temp = *pObj;

Note that you need to do it in two steps and not

  MyObject temp = *pObj;

which will call the copy constructor instead:

  MyObject(const MyObject& rhs);


0
 

Author Comment

by:engllf
ID: 1181049
Hi thui,

Then do i need to overload the operator= if i am assigning pointers?

engllf

0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1181050
You can only overload operator= for classes you define. You cannot override assignment operators for pointer to classes. For example this is possible to override because the left hand side is an object:

  MyObject temp;
  MyObject o1;
  MyObject* p2;
  temp = o1;   // MyObject::operator = (MyObject rhs);
  temp = p2;   // MyObject::operator = (MyObject* rhs);

However, these are NOT possible because the left hand side of the = is a pointer:

  MyObject* pTemp2;
  MyObject o1;
  MyObject* p2;

  pTemp2 = o1;  // Not possible
  pTemp2 = p2;  // Not possible

But this is because *pTemp2 is an object:

  *pTemp2 = o1;
  *pTemp2 = p2;

  MyObject* pTemp;
   
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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