tags:

views:

409

answers:

5

i know you can make two objects equal to each other when one of them is being declared. i tested this in my program. but when i went to use a assignment statement it freaked out. Can you make two objects equal to each other with a assignment statement or can you only do that when one object is being declared?

+2  A: 

Overloading assignment operator for that object can help you. (I hope you are talking about objects of same class :))

Aviator
+5  A: 

You have provide operator= to a class so as copy the contents of another object. For example:

class A
{
  public:

   //Default constructor
   A();

   //Copy constructor
   A(const A&);

   //Assignment operator
    A& operator=(const A& a);
};

int main()
{
  A a; //Invokes default constructor
  A b(a); //Invokes copy constructor;
  A c;
  c = a; //Invokes assignment operator
}
Naveen
But you can use assignment (c = a) without overloading = operator
Ahmed Said
Yes, that's why I wrote it as two different statements
Naveen
@Ahmed Said: If you do not explicitly define one then the compiler generates one automatically for you.
Martin York
A: 

For assignment operator just overload assignment operator according to your class implementation.

GG
A: 

This answer applies to C#.

Along with Overloading = operator you should also override equals method. You should also check Guidelines for Overloading Equals() and Operator ==

public struct Complex 
{
   double re, im;
   public override bool Equals(Object obj) 
   {
      return obj is Complex && this == (Complex)obj;
   }
   public override int GetHashCode() 
   {
      return re.GetHashCode() ^ im.GetHashCode();
   }
   public static bool operator ==(Complex x, Complex y) 
   {
      return x.re == y.re && x.im == y.im;
   }
   public static bool operator !=(Complex x, Complex y) 
   {
      return !(x == y);
   }
}
Mahin
It's tagged C++, and that's not C++ for sure ;)
Anteru
Oops.. did not noticed... thanks to pint it out.. :)+1 to pint it out. :)
Mahin
pint = point ( in above comment ) :)
Mahin
A: 

The object may be required to initialize or create or equated with another object when the object is dependent on other objects.

In this case, copy constructor gives the best solution..because it does not copy the object to other object by bit by bit. Bitwise copy creates problem if the memory is allocated dynamically to the object. so, the solutions is the defining the copy_constructor in the class.The copy constuctor takes reference to an existing object of the same type as its argument, and it is used to create a new object from an existing one. Here is an examplet to equate the objects with other objects using copy constructor..

#include "stdafx.h"
#include "iostream"
using namespace std;
class array
{
int *ptr;
int size;
public:
array (int sz) {
ptr = new int [sz];
size =sz;
for(int index=0;index<size;index++)
ptr[index]=size;
}
~array(){
delete[] ptr;
}
array(array &a) {
int index;
ptr=new int[a.size];
for(index=0;index<a.size;index++)
ptr[index]=a.ptr[index];
cout<<"copy_constructor invoked"<<endl;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
array num(10);
array x(num); //invokes copy_constructor
array y=num; //invokes copy consturctor

return 0;
}
Red
some how, the code is not able to insert line by line.. i am not sure
Red