Solved

Overloading =

Posted on 1998-02-06
3
262 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
Comment Utility
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
Comment Utility
Hi thui,

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

engllf

0
 
LVL 15

Expert Comment

by:Tommy Hui
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

16 Experts available now in Live!

Get 1:1 Help Now