Multiple choice technology programming languages

In Java ,the statement Foo d = c; binds d to reference the same object as c, what will be equivalent statement in C++

  1. Foo d = c;

  2. Foo *d = c;

  3. Foo d; d=c;

  4. --

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In Java, object variables are references by default, so Foo d = c; makes d reference the same object as c. In C++, the equivalent behavior requires using a pointer: Foo *d = &c; (note the address-of operator). However, in the context of this question's options, Foo *d = c; is the closest C++ pointer declaration equivalent, as C++ pointers are used to achieve reference-like behavior. Option A would be value semantics (copy), not reference semantics.