3

I got this question wrong on a midterm in my Computer Science class and am trying to find the correct answer, but I've been searching for a while and haven't turned up anything that conclusively states the answer. These are the options the teacher gave:

A) ==

B) .

C) =

D) b and c above

E) a, b, and c above

The answer I marked (incorrectly) is B, and I know the correct answer must include B as this operator is always used with classes without any overloading. However, one of the other options must also be correct, but I don't know if it's only one or both of them. I know = will give a shallow copy, so I'm assuming that is included in the answer. But does == do anything at all if not overloaded?

All in all, I'm guessing the correct answer is D, but I'd like someone to back up this claim, and hopefully point me in the direction of some documentation online about this as I can't seem to find it.

Bobazonski
  • 1,515
  • 5
  • 26
  • 43

2 Answers2

0

The correct answer is D (and B and C if you wish so). operator== has to be overloaded to be useable with user defined classes.

Just imagine some code:

class A {
    void foo() {...}
};

A a;
a.foo(); // you have seen this, alright

A b;
a = b; // this does a shallow copy or uses your assignment constructor

A c = b // this does a shallow copy or uses your copy constructor

bool issame = (a == b); // gives an compiler ERROR if operator== is not overloaded for A
Danvil
  • 22,240
  • 19
  • 65
  • 88
-1

The correct answer is B.

== needs to be user-defined.

= is either a (copy/move) constructor or assignment operator depending on the context. The copy/move constructor is usually generated by the compiler if not defined, but there are specific circumstances in which it is not. In any case, it is not "built-in" for any meaning of that word. All of them can be "inhibited" in some sense.

Community
  • 1
  • 1
rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • `=` is "built-in" in the sense that the code works without defining an `operator=`. – Danvil Mar 26 '14 at 13:57
  • @Danvil not always, because `=` can mean many things, and code might not work in certain cases because the function isn't generated at all. – rubenvb Mar 26 '14 at 13:58
  • I would be interested in a case where `operator=` does not work for class instances. – Danvil Mar 26 '14 at 14:00
  • @Danvil [A simple, albeit contrived, but not extremely impossible example](http://coliru.stacked-crooked.com/a/de32060aebe56897) – rubenvb Mar 26 '14 at 14:51
  • A somewhat more common example of the above [here](http://coliru.stacked-crooked.com/a/36de9b3117eecee0). – rubenvb Mar 26 '14 at 14:58
  • In both examples you have actively denied to use the auto-generated implementation which the compiler would have created for you. One might argue that this does not count ... – Danvil Mar 26 '14 at 15:22
  • @Danvil The fact that this can happen means it's not "built-in", barring a better definition of "built-in". – rubenvb Mar 26 '14 at 15:33
  • "built-in" is horribly undefined ... – Danvil Mar 26 '14 at 15:35