Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a object equal to another object

Tags:

c++

object

equals

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?

like image 920
TheFuzz Avatar asked Jan 24 '23 05:01

TheFuzz


1 Answers

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
}
like image 122
Naveen Avatar answered Jan 25 '23 23:01

Naveen