making a object equal to another object
25,064
Solution 1
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
}
Solution 2
Overloading assignment operator for that object can help you. (I hope you are talking about objects of same class :))
Author by
TheFuzz
Updated on July 09, 2022Comments
-
TheFuzz almost 2 years
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?
-
Ahmed over 14 yearsBut you can use assignment (c = a) without overloading = operator
-
Kiran Kumar over 14 yearsYes, that's why I wrote it as two different statements
-
Martin York over 14 years@Ahmed Said: If you do not explicitly define one then the compiler generates one automatically for you.
-
Red over 14 yearssome how, the code is not able to insert line by line.. i am not sure