5

this was a recent T/F question that came up in my cs course that I found a bit confusing.

The textbook states:

The = operator may be used to assign one object's data to another object, or to initialize one object with another object's data. By default, each member of one object is copied to its counterpart in the other object.

The question verbatim was:

You cannot use the = operator to assign one object's values to another object, unless you overload the operator. T/F?

Judging from that particular paragraph of the textbook, I answered false. However, it turns out the quiz answer was actually true.

When I looked up the question online, I saw other sources listing the answer as "false" as well. Granted, these were just generic flashcard / quiz websites, so I don't put much stock in them.

Basically, I'm just curious what the real answer is for future studying purposes.


P.S.: The textbook later goes on to state: "In order to change the way the assignment operator works, it must be overloaded. Operator overloading permits you to redefine an existing operator’s behavior when used with a class object."

I feel like this is related and supports the "true" answer, but I'm not really sure.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Random Coder
  • 105
  • 1
  • 6

5 Answers5

2

The statement

You cannot use the = operator to assign one object's values to another object, unless you overload the operator.

… is trivially false, because you can assign to e.g. an int variable. Which is an object. In C++.

Possibly they intended to write “class-type object”, but even so it's false. E.g. any POD (Plain Old Data) class type object is assignable and lacks an overloaded assignment operator.

However, there are cases where you want or need to prohibit or take charge of assignment.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
2

If you don't implement the assignment operator yourself, the compiler will generate one for you, which will copy the data from the source to the destination, invoking assignment operators of the members of your class where possible/necessary.

This does not work if your class e.g. features const members, and it does not yield the desired result if your class contains pointers to dynamically allocated objects.

So, I would also say that the statement is false.

Rene
  • 2,466
  • 1
  • 12
  • 18
1

That's a trick question. The syntactic answer is "false" but the semantic answer is "maybe".

struct Foo
{
   int a;
   double b;
};

Foo foo1;
Foo foo2;
foo2 = foo1;    // Ok in both senses.

struct Bar
{
   Bar() : arr(new int[20]) {}
   ~Bar() { delete [] arr; }
   int* arr;
};

Bar bar1;
Bar bar2;
bar2 = bar1;  // Ok in syntax not in semantics
              // This will lead to UB
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

In C++, when User-Defined Types such as class and struct are involved then its better to overload operator= for the Type. Using default assignment operator with object of class or struct will involve Shallow Copy. Shallow Copy sometimes lead to undefined behavior when the class or struct objects have dynamically allocated memory inside them.

Appropriately overloading operator= for class and struct types will lead to Deep Copy which is the correct method of assigning an object ObjA to object ObjB (ObjB = ObjA) when ObjA and ObjB are objects of some class or struct and contain dynamically allocated memory inside them.

The = operator may be used to assign one object's data to another object, or to initialize one object with another object's data. By default, each member of one object is copied to its counterpart in the other object. This is true when object of class/struct has only static fundamental type of data inside it. Here,

  • by static it means that all the memory requirement of object is fixed at compile time only.

You cannot use the default = operator to assign one object's values to another object, unless you overload the operator. This is true when object of class/struct has some dynamically allocated memory inside it, possibly by using pointers.

For more reference see: What is the difference between a deep copy and a shallow copy?

Community
  • 1
  • 1
sameerkn
  • 2,209
  • 1
  • 12
  • 13
0

You cannot use the = operator to assign one object's values to another object, unless you overload the operator. T/F?

Ans: F

You can assign one object's values to another object without overloading = operator, as compiler by default defines assignment operator for the class in all case except for below

  • you have explicitly declared a copy-assignment operator (for class X an operator = taking X, X& or const X&) )
  • there is a member in your class that is not assignable (such as a reference, a const object or a class with no or inaccessible assignment operator)

But you need to understand why it becomes mandatory to overload the assignment operator in case you are having user defined members or having dynamic memory allocation,for this read concept of shallow and deep copy

Community
  • 1
  • 1
Ajay
  • 775
  • 5
  • 19