Стандарт C++ 11 (1119564), страница 66
Текст из файла (страница 66)
Two standard-layout structs share a common initialsequence if corresponding members have layout-compatible types and either neither member is a bit-field orboth are bit-fields with the same width for a sequence of one or more initial members.20A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to itsinitial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note:There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning,as necessary to achieve appropriate alignment.
— end note ]9.3Member functions[class.mfct]1Functions declared in the definition of a class, excluding those declared with a friend specifier (11.3), arecalled member functions of that class. A member function may be declared static in which case it is a staticmember function of its class (9.4); otherwise it is a non-static member function of its class (9.3.1, 9.3.2).2A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2), or it may be defined outside of its class definition if it has already been declared but not definedin its class definition. A member function definition that appears outside of the class definition shall appearin a namespace scope enclosing the class definition.
Except for member function definitions that appearoutside of a class definition, and except for explicit specializations of member functions of class templatesand member function templates (14.7) appearing outside of the class definition, a member function shall notbe redeclared.3An inline member function (whether static or non-static) may also be defined outside of its class definitionprovided either its declaration in the class definition or its definition outside of the class definition declaresthe function as inline.
[ Note: Member functions of a class in namespace scope have external linkage.Member functions of a local class (9.8) have no linkage. See 3.5. — end note ]4There shall be at most one definition of a non-inline member function in a program; no diagnostic is required.There may be more than one inline member function definition in a program. See 3.2 and 7.1.2.§ 9.3222© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)5If the definition of a member function is lexically outside its class definition, the member function nameshall be qualified by its class name using the :: operator. [ Note: A name used in a member functiondefinition (that is, in the parameter-declaration-clause including the default arguments (8.3.6) or in themember function body) is looked up as described in 3.4.
— end note ] [ Example:struct X {typedef int T;static T count;void f(T);};void X::f(T t = count) { }The member function f of class X is defined in global scope; the notation X::f specifies that the function fis a member of class X and in the scope of class X. In the function definition, the parameter type T refers tothe typedef member T declared in class X and the default argument count refers to the static data membercount declared in class X.
— end example ]6A static local variable in a member function always refers to the same object, whether or not the memberfunction is inline.7Previously declared member functions may be mentioned in friend declarations.8Member functions of a local class shall be defined inline in their class definition, if they are defined at all.9[ Note: A member function can be declared (but not defined) using a typedef for a function type. Theresulting member function has exactly the same type as it would have if the function declarator wereprovided explicitly, see 8.3.5. For example,typedef void fv(void);typedef void fvc(void) const;struct S {fv memfunc1;// equivalent to: void memfunc1(void);void memfunc2();fvc memfunc3;// equivalent to: void memfunc3(void) const;};fv S::* pmfv1 = &S::memfunc1;fv S::* pmfv2 = &S::memfunc2;fvc S::* pmfv3 = &S::memfunc3;Also see 14.3.
— end note ]9.3.1Nonstatic member functions[class.mfct.non-static]1A non-static member function may be called for an object of its class type, or for an object of a class derived(Clause 10) from its class type, using the class member access syntax (5.2.5, 13.3.1.1). A non-static memberfunction may also be called directly using the function call syntax (5.2.2, 13.3.1.1) from within the body ofa member function of its class or of a class derived from its class.2If a non-static member function of a class X is called for an object that is not of type X, or of a type derivedfrom X, the behavior is undefined.3When an id-expression (5.1) that is not part of a class member access syntax (5.2.5) and not used to forma pointer to member (5.3.1) is used in a member of class X in a context where this can be used (5.1.1),if name lookup (3.4) resolves the name in the id-expression to a non-static non-type member of some classC, and if either the id-expression is potentially evaluated or C is X or a base class of X, the id-expression istransformed into a class member access expression (5.2.5) using (*this) (9.3.2) as the postfix-expression tothe left of the .
operator. [ Note: If C is not X or a base class of X, the class member access expression is§ 9.3.1© ISO/IEC 2011 – All rights reserved223ISO/IEC 14882:2011(E)ill-formed. — end note ] Similarly during name lookup, when an unqualified-id (5.1) used in the definition ofa member function for class X resolves to a static member, an enumerator or a nested type of class X or of abase class of X, the unqualified-id is transformed into a qualified-id (5.1) in which the nested-name-specifiernames the class of the member function. [ Example:struct tnode {char tword[20];int count;tnode *left;tnode *right;void set(const char*, tnode* l, tnode* r);};void tnode::set(const char* w, tnode* l, tnode* r) {count = strlen(w)+1;if (sizeof(tword)<=count)perror("tnode string too long");strcpy(tword,w);left = l;right = r;}void f(tnode n1, tnode n2) {n1.set("abc",&n2,0);n2.set("def",0,0);}In the body of the member function tnode::set, the member names tword, count, left, and right refer tomembers of the object for which the function is called.
Thus, in the call n1.set("abc",&n2,0), tword refersto n1.tword, and in the call n2.set("def",0,0), it refers to n2.tword. The functions strlen, perror,and strcpy are not members of the class tnode and should be declared elsewhere.110 — end example ]4A non-static member function may be declared const, volatile, or const volatile. These cv-qualifiersaffect the type of the this pointer (9.3.2). They also affect the function type (8.3.5) of the member function;a member function declared const is a const member function, a member function declared volatile isa volatile member function and a member function declared const volatile is a const volatile memberfunction. [ Example:struct X {void g() const;void h() const volatile;};X::g is a const member function and X::h is a const volatile member function. — end example ]5A non-static member function may be declared with a ref-qualifier (8.3.5); see 13.3.1.6A non-static member function may be declared virtual (10.3) or pure virtual (10.4).9.3.21The this pointer[class.this]In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose valueis the address of the object for which the function is called.
The type of this in a member function ofa class X is X*. If the member function is declared const, the type of this is const X*, if the member110) See, for example, <cstring> (21.7).§ 9.3.2224© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)function is declared volatile, the type of this is volatile X*, and if the member function is declaredconst volatile, the type of this is const volatile X*.2In a const member function, the object for which the function is called is accessed through a const accesspath; therefore, a const member function shall not modify the object and its non-static data members.[ Example:struct s {int a;int f() const;int g() { return a++; }int h() const { return a++; } // error};int s::f() const { return a; }The a++ in the body of s::h is ill-formed because it tries to modify (a part of) the object for which s::h()is called.
This is not allowed in a const member function because this is a pointer to const; that is, *thishas const type. — end example ]3Similarly, volatile semantics (7.1.6.1) apply in volatile member functions when accessing the object andits non-static data members.4A cv-qualified member function can be called on an object-expression (5.2.5) only if the object-expression isas cv-qualified or less-cv-qualified than the member function.
[ Example:void k(s& x, const s& y) {x.f();x.g();y.f();y.g();}// errorThe call y.g() is ill-formed because y is const and s::g() is a non-const member function, that is, s::g()is less-qualified than the object-expression y. — end example ]5Constructors (12.1) and destructors (12.4) shall not be declared const, volatile or const volatile. [ Note:However, these functions can be invoked to create and destroy objects with cv-qualified types, see (12.1)and (12.4). — end note ]9.4Static members[class.static]1A data or function member of a class may be declared static in a class definition, in which case it is astatic member of the class.2A static member s of class X may be referred to using the qualified-id expression X::s; it is not necessary touse the class member access syntax (5.2.5) to refer to a static member.
A static member may be referredto using the class member access syntax, in which case the object expression is evaluated. [ Example:struct process {static void reschedule();};process& g();void f() {process::reschedule();g().reschedule();}// OK: no object necessary// g() is called§ 9.4© ISO/IEC 2011 – All rights reserved225ISO/IEC 14882:2011(E)— end example ]3A static member may be referred to directly in the scope of its class or in the scope of a class derived(Clause 10) from its class; in this case, the static member is referred to as if a qualified-id expression wasused, with the nested-name-specifier of the qualified-id naming the class scope from which the static memberis referenced.
[ Example:int g();struct Xstatic};struct Ystatic};int Y::i{int g();: X {int i;= g();// equivalent to Y::g();— end example ]4If an unqualified-id (5.1) is used in the definition of a static member following the member’s declarator-id,and name lookup (3.4.1) finds that the unqualified-id refers to a static member, enumerator, or nestedtype of the member’s class (or of a base class of the member’s class), the unqualified-id is transformed intoa qualified-id expression in which the nested-name-specifier names the class scope from which the memberis referenced.