+ 1

General question.

I see this used in some challenges, but don't quite understand it. 2 classes are made and defined then, Ex. What is the output? A obj1; B obj2; obj1 = obj2; cout << obj1; So what Im not understanding is how an object can be set to a value like a variable. Or is something else happening here? How does this work? Apologies if my question seems noobish, it probably is. Thanks in advance.

14th Aug 2017, 4:13 AM
John Drennan
John Drennan - avatar
5 Answers
+ 1
It's not quite clear to me what you're asking, but an object is an implementation of some type, located in memory. A variable is a named place in memory. So an object may be placed in a variable (assigned to it). If you're asking how an object of a class can be assigned to another object of a class, the answer to it is - implicitly using an assignment operator (or copy consrtuctor). A default implementation of copy constructor and assignment operator is implicitly provided by C++, and creates a deep copy of an object - just copies all the members.
14th Aug 2017, 4:44 AM
deFault
+ 1
so basically A obj1; B obj2; obj1=obj2; is the same as B obj1; B obj2; ?
14th Aug 2017, 4:53 AM
John Drennan
John Drennan - avatar
+ 1
The only thing I can tell you about this subect, which is not much, but what I do know is this, that c++ compilers have a built in operater overloading with the assignment operator(=) , or else you have to create operater loading for the assignment operator comes to objects.
14th Aug 2017, 5:08 AM
Rick Zalman
+ 1
Not always the same. It depends on the types A and B. It may even result in a compile time error if A and B aren't compatible.
14th Aug 2017, 5:33 AM
deFault
+ 1
I agree with Vincent, that the objects have to be the same type that is they have to be from the same class or derived class. Then operater overloading can occur. and that's as far as my knowledge goes on this subect, which is not much help I'm afraid. But I'm still learning which goes a long way , in improving ones knowledge.
14th Aug 2017, 6:21 AM
Rick Zalman