Solved

Overloading =

Posted on 1998-02-06
3
273 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

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
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.

821 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