Init
This commit is contained in:
87
cppdraft/class/expl/init.md
Normal file
87
cppdraft/class/expl/init.md
Normal file
@@ -0,0 +1,87 @@
|
||||
[class.expl.init]
|
||||
|
||||
# 11 Classes [[class]](./#class)
|
||||
|
||||
## 11.9 Initialization [[class.init]](class.init#class.expl.init)
|
||||
|
||||
### 11.9.2 Explicit initialization [class.expl.init]
|
||||
|
||||
[1](#1)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5369)
|
||||
|
||||
An object of class type can be initialized with a parenthesized[*expression-list*](expr.post.general#nt:expression-list "7.6.1.1 General [expr.post.general]"),
|
||||
where the[*expression-list*](expr.post.general#nt:expression-list "7.6.1.1 General [expr.post.general]") is construed as an argument list for a constructor
|
||||
that is called to initialize the object[.](#1.sentence-1)
|
||||
|
||||
Alternatively, a single[*assignment-expression*](expr.assign#nt:assignment-expression "7.6.19 Assignment and compound assignment operators [expr.assign]") can be specified as an[*initializer*](dcl.init.general#nt:initializer "9.5.1 General [dcl.init.general]") using the= form of initialization[.](#1.sentence-2)
|
||||
|
||||
Either direct-initialization semantics or copy-initialization semantics apply;
|
||||
see [[dcl.init]](dcl.init "9.5 Initializers")[.](#1.sentence-3)
|
||||
|
||||
[*Example [1](#example-1)*: struct complex { complex();
|
||||
complex(double);
|
||||
complex(double,double);};
|
||||
|
||||
complex sqrt(complex,complex);
|
||||
|
||||
complex a(1); // initialized by calling complex(double) with argument 1 complex b = a; // initialized as a copy of a complex c = complex(1,2); // initialized by calling complex(double,double) with arguments 1 and 2 complex d = sqrt(b,c); // initialized by calling sqrt(complex,complex) with d as its result object complex e; // initialized by calling complex() complex f = 3; // initialized by calling complex(double) with argument 3 complex g = { 1, 2 }; // initialized by calling complex(double, double) with arguments 1 and 2 â *end example*]
|
||||
|
||||
[*Note [1](#note-1)*:
|
||||
|
||||
Overloading of the assignment operator ([[over.assign]](over.assign "12.4.3.2 Simple assignment"))
|
||||
has no effect on initialization[.](#1.sentence-4)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[2](#2)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5410)
|
||||
|
||||
An object of class type can also be initialized by a[*braced-init-list*](dcl.init.general#nt:braced-init-list "9.5.1 General [dcl.init.general]")[.](#2.sentence-1)
|
||||
|
||||
List-initialization semantics apply;
|
||||
see [[dcl.init]](dcl.init "9.5 Initializers") and [[dcl.init.list]](dcl.init.list "9.5.5 List-initialization")[.](#2.sentence-2)
|
||||
|
||||
[*Example [2](#example-2)*: complex v[6] = { 1, complex(1,2), complex(), 2 };
|
||||
|
||||
Here,complex::complex(double) is called for the initialization ofv[0] andv[3],complex::complex(double, double) is called for the initialization ofv[1],complex::complex() is called for the initialization ofv[2],v[4],
|
||||
andv[5][.](#2.sentence-3)
|
||||
|
||||
For another example,
|
||||
|
||||
struct X {int i; float f;
|
||||
complex c;} x = { 99, 88.8, 77.7 };
|
||||
|
||||
Here,x.i is initialized with 99,x.f is initialized with 88.8, andcomplex::complex(double) is called for the initialization ofx.c[.](#2.sentence-5)
|
||||
|
||||
â *end example*]
|
||||
|
||||
[*Note [2](#note-2)*:
|
||||
|
||||
Braces can be elided in the[*initializer-list*](dcl.init.general#nt:initializer-list "9.5.1 General [dcl.init.general]") for any aggregate, even if the aggregate has members of a class type with
|
||||
user-defined type conversions; see [[dcl.init.aggr]](dcl.init.aggr "9.5.2 Aggregates")[.](#2.sentence-6)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[3](#3)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5462)
|
||||
|
||||
[*Note [3](#note-3)*:
|
||||
|
||||
IfT is a class type with no default constructor,
|
||||
any initializing declaration of an object of typeT (or array thereof) is ill-formed if no[*initializer*](dcl.init.general#nt:initializer "9.5.1 General [dcl.init.general]") is explicitly specified (see [[class.init]](class.init "11.9 Initialization") and [[dcl.init]](dcl.init "9.5 Initializers"))[.](#3.sentence-1)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[4](#4)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5474)
|
||||
|
||||
[*Note [4](#note-4)*:
|
||||
|
||||
The order in which objects with static or thread storage duration
|
||||
are initialized is described in [[basic.start.dynamic]](basic.start.dynamic "6.10.3.3 Dynamic initialization of non-block variables") and [[stmt.dcl]](stmt.dcl "8.10 Declaration statement")[.](#4.sentence-1)
|
||||
|
||||
â *end note*]
|
||||
Reference in New Issue
Block a user