Search code examples
c++reference-typemember-variables

Part of an object or reference


Assuming a class A has a member variable(which is an object rather than a reference) m. Naturally I will think that: When I defined an object 'o1', then the Expression 'o1.m' is an object type either; When I defined a reference 'q1', then the Expression 'q1.m' is a reference type either.

Is that conclusion correct? I'm wondering what any relative clauses are in C++ standard document if it is true(I've been searching for it/them for one more days already but got almost all about 'reference as member of class' so far)?


Solution

  • Take a look at §5.2.5 (Class member access in Postfix Expressions, from N3797 C++14 Draft):

    Abbreviating postfix-expression.id-expression as E1.E2, E1 is called the object expression. The type and value category of E1.E2 are determined as follows. In the remainder of 5.2.5, cq represents either const or the absence of const and vq represents either volatile or the absence of volatile. cv represents an arbitrary set of cv-qualifiers, as defined in 3.9.3.

    If E2 is declared to have type “reference to T,” then E1.E2 is an lvalue; the type of E1.E2 is T. Otherwise, one of the following rules applies.

    • If E2 is a non-static data member and the type of E1 is “cq1 vq1 X”, and the type of E2 is “cq2 vq2 T”, the expression designates the named member of the object designated by the first expression. If E1 is an lvalue, then E1.E2 is an lvalue; otherwise E1.E2 is an xvalue. Let the notation vq12 stand for the “union” of vq1 and vq2 ; that is, if vq1 or vq2 is volatile, then vq12 is volatile. Similarly, let the notation cq12 stand for the “union” of cq1 and cq2 ; that is, if cq1 or cq2 is const, then cq12 is const. If E2 is declared to be a mutable member, then the type of E1.E2 is “vq12 T”. If E2 is not declared to be a mutable member, then the type of E1.E2 is “cq12 vq12 T”.

    The standard says nothing about E2 becoming a reference even if E1 is. To be explicit, if E1 is a reference type and E2 is not, E1.E2 is not a reference type.