Solved

Overloading =

Posted on 1998-02-06
3
277 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
[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
  • 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

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
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.

739 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