?
Solved

Overloading =

Posted on 1998-02-06
3
Medium Priority
?
286 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 40 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

719 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