Стандарт C++ 11 (1119564), страница 73
Текст из файла (страница 73)
[ Example:class B {public:virtual int f();};class D : public B {private:int f();};void f() {D d;B* pb = &d;D* pd = &d;pb->f();// OK: B::f() is public,§ 11.5© ISO/IEC 2011 – All rights reserved255ISO/IEC 14882:2011(E)pd->f();// D::f() is invoked// error: D::f() is private}— end example ]2Access is checked at the call point using the type of the expression used to denote the object for which themember function is called (B* in the example above).
The access of the member function in the class inwhich it was defined (D in the example above) is in general not known.11.61Multiple access[class.paths]If a name can be reached by several paths through a multiple inheritance graph, the access is that of thepath that gives most access. [ Example:class W { public: void f(); };class A : private virtual W { };class B : public virtual W { };class C : public A, public B {void f() { W::f(); }// OK};2Since W::f() is available to C::f() along the public path through B, access is allowed. — end example ]11.71Nested classes[class.access.nest]A nested class is a member and as such has the same access rights as any other member.
The members ofan enclosing class have no special access to members of a nested class; the usual access rules (Clause 11)shall be obeyed. [ Example:class E {int x;class B { };class I {B b;int y;void f(E* p, int i) {p->x = i;}};int g(I* p) {return p->y;}};// OK: E::I can access E::B// OK: E::I can access E::x// error: I::y is private— end example ]§ 11.7256© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)12Special member functions[special]1The default constructor (12.1), copy constructor and copy assignment operator (12.8), move constructorand move assignment operator (12.8), and destructor (12.4) are special member functions. [ Note: Theimplementation will implicitly declare these member functions for some class types when the program doesnot explicitly declare them.
The implementation will implicitly define them if they are odr-used (3.2).See 12.1, 12.4 and 12.8. — end note ] Programs shall not define implicitly-declared special member functions.2Programs may explicitly refer to implicitly-declared special member functions. [ Example: a program mayexplicitly call, take the address of or form a pointer to member to an implicitly-declared special memberfunction.struct A { };struct B : A {B& operator=(const B &);};B& B::operator=(const B& s) {this->A::operator=(s);return *this;}// implicitly declared A::operator=// well formed— end example ]3[ Note: The special member functions affect the way objects of class type are created, copied, moved, anddestroyed, and how values can be converted to values of other types.
Often such special member functionsare called implicitly. — end note ]4Special member functions obey the usual access rules (Clause 11). [ Example: declaring a constructorprotected ensures that only derived classes and friends can create objects using it. — end example ]12.11Constructors[class.ctor]Constructors do not have names.
A special declarator syntax is used to declare or define the constructor.The syntax uses:— an optional decl-specifier-seq in which each decl-specifier is either a function-specifier or constexpr,— the constructor’s class name, and— a parameter listin that order. In such a declaration, optional parentheses around the constructor class name are ignored.[ Example:struct S {S();};// declares the constructorS::S() { }// defines the constructor— end example ]§ 12.1© ISO/IEC 2011 – All rights reserved257ISO/IEC 14882:2011(E)2A constructor is used to initialize objects of its class type. Because constructors do not have names, they arenever found during name lookup; however an explicit type conversion using the functional notation (5.2.3)will cause a constructor to be called to initialize an object.
[ Note: For initialization of objects of class typesee 12.6. — end note ]3A typedef-name shall not be used as the class-name in the declarator-id for a constructor declaration.4A constructor shall not be virtual (10.3) or static (9.4). A constructor can be invoked for a const,volatile or const volatile object. A constructor shall not be declared const, volatile, or constvolatile (9.3.2). const and volatile semantics (7.1.6.1) are not applied on an object under construction.They come into effect when the constructor for the most derived object (1.8) ends.
A constructor shall notbe declared with a ref-qualifier.5A default constructor for a class X is a constructor of class X that can be called without an argument. Ifthere is no user-declared constructor for class X, a constructor having no parameters is implicitly declaredas defaulted (8.4). An implicitly-declared default constructor is an inline public member of its class. Adefaulted default constructor for class X is defined as deleted if:— X is a union-like class that has a variant member with a non-trivial default constructor,— any non-static data member with no brace-or-equal-initializer is of reference type,— any non-variant non-static data member of const-qualified type (or array thereof) with no brace-orequal-initializer does not have a user-provided default constructor,— X is a union and all of its variant members are of const-qualified type (or array thereof),— X is a non-union class and all members of any anonymous union member are of const-qualified type(or array thereof),— any direct or virtual base class, or non-static data member with no brace-or-equal-initializer, has classtype M (or array thereof) and either M has no default constructor or overload resolution (13.3) as appliedto M’s default constructor results in an ambiguity or in a function that is deleted or inaccessible fromthe defaulted default constructor, or— any direct or virtual base class or non-static data member has a type with a destructor that is deletedor inaccessible from the defaulted default constructor.A default constructor is trivial if it is not user-provided and if:— its class has no virtual functions (10.3) and no virtual base classes (10.1), and— no non-static data member of its class has a brace-or-equal-initializer, and— all the direct base classes of its class have trivial default constructors, and— for all the non-static data members of its class that are of class type (or array thereof), each such classhas a trivial default constructor.Otherwise, the default constructor is non-trivial.6A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odrused (3.2) to create an object of its class type (1.8) or when it is explicitly defaulted after its first declaration.The implicitly-defined default constructor performs the set of initializations of the class that would beperformed by a user-written default constructor for that class with no ctor-initializer (12.6.2) and an emptycompound-statement.
If that user-written default constructor would be ill-formed, the program is ill-formed.If that user-written default constructor would satisfy the requirements of a constexpr constructor (7.1.5),the implicitly-defined default constructor is constexpr. Before the defaulted default constructor for a§ 12.1258© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)class is implicitly defined, all the non-user-provided default constructors for its base classes and its nonstatic data members shall have been implicitly defined.
[ Note: An implicitly-declared default constructorhas an exception-specification (15.4). An explicitly-defaulted definition might have an implicit exceptionspecification, see 8.4. — end note ]7Default constructors are called implicitly to create class objects of static, thread, or automatic storageduration (3.7.1, 3.7.2, 3.7.3) defined without an initializer (8.5), are called to create class objects of dynamicstorage duration (3.7.4) created by a new-expression in which the new-initializer is omitted (5.3.4), orare called when the explicit type conversion syntax (5.2.3) is used. A program is ill-formed if the defaultconstructor for an object is implicitly used and the constructor is not accessible (Clause 11).8[ Note: 12.6.2 describes the order in which constructors for base classes and non-static data members arecalled and describes how arguments can be specified for the calls to these constructors.
— end note ]9A copy constructor (12.8) is used to copy objects of class type. A move constructor (12.8) is used to movethe contents of objects of class type.10No return type (not even void) shall be specified for a constructor. A return statement in the body of aconstructor shall not specify a return value. The address of a constructor shall not be taken.11A functional notation type conversion (5.2.3) can be used to create new objects of its type. [ Note: Thesyntax looks like an explicit call of the constructor. — end note ] [ Example:complex zz = complex(1,2.3);cprint( complex(7.8,1.2) );— end example ]12An object created in this way is unnamed. [ Note: 12.2 describes the lifetime of temporary objects.
— endnote ] [ Note: Explicit constructor calls do not yield lvalues, see 3.10. — end note ]13[ Note: some language constructs have special semantics when used during construction; see 12.6.2 and 12.7.— end note ]14During the construction of a const object, if the value of the object or any of its subobjects is accessedthrough a glvalue that is not obtained, directly or indirectly, from the constructor’s this pointer, the valueof the object or subobject thus obtained is unspecified. [ Example:struct C;void no_opt(C*);struct C {int c;C() : c(0) { no_opt(this); }};const C cobj;void no_opt(C* cptr) {int i = cobj.c * 100;cptr->c = 1;cout << cobj.c * 100<< ’\n’;}// value of cobj.c is unspecified// value of cobj.c is unspecified— end example ]§ 12.1© ISO/IEC 2011 – All rights reserved259ISO/IEC 14882:2011(E)12.2Temporary objects[class.temporary]1Temporaries of class type are created in various contexts: binding a reference to a prvalue (8.5.3), returninga prvalue (6.6.3), a conversion that creates a prvalue (4.1, 5.2.9, 5.2.11, 5.4), throwing an exception (15.1),entering a handler (15.3), and in some initializations (8.5).