-3

I have base class Aclass and derived class Bclass. If I create object Bclass b and if I write A &aref(b) is &aref now object of a class A or class B?

Lamija37
  • 7
  • 4
  • A reference is a reference and not an object. –  Jun 14 '17 at 13:13
  • but reference on what? – Lamija37 Jun 14 '17 at 13:21
  • On whatever you made it point to, b in this case. No copy was made. A reference is like a pointer with limited power, and some (but not much) added safety. By convention, references are used as pointers that should not be null (even though it's possible to make it so) when passed as parameters, or to hold a pointer to a known good object to make code more readable while avoiding to make a copy. – Michaël Roy Jun 14 '17 at 13:39

1 Answers1

0

&aref now object of a class A or class B?

It is a bit ambiguous what you mean by &aref. Regardless how it can be interpreted, it is neither an object of A nor B.

aref is a variable. The type of the variable is reference. It is not an object. More specifically, it is a reference to type A. However, it refers to an object of type B.

&aref on the other hand is an expression using the address-of operator (&). The type of this expression is a pointer, and the value is the address of the object referred by aref.


More technically, aref actually directly refers to the object of type A that is the parent subobject of an object of type B. But from the object oriented perspective, there is no difference.

eerorika
  • 232,697
  • 12
  • 197
  • 326