- also takes place during function calls: function parameters and the function return values are also initialized
- (remember - values are copied)
- no-arg constructors
- call like
Point p;(no parentheses) - with parentheses it's a function declaration
- call like
Point p1; // calls no-arg ctor, NEVER USE Point p1(); - it's a function call
Point p1(1, 2); // calls ctor-
calls copy ctor
Point p2 = p1; // variable init Point foo() { Point(1, 2) p; return p } // function return value foo(p1); // function parameter Point p3(p2); // direct call of copy ctor
-
**note: **copy ctor is not called for unscoped (unnamed) temporary objects
Point p2 = Point(1, 2); Point foo() { return Point(1, 2) } foo(Point(1, 2));
- a bit like Java's
POJO(Plain Old Java Object) - aggregate - array or class with
- no user-declared constructors (
defaultconstructor is allowed), - no private or protected non-static data members,
- no base classes,
- and no virtual functions
- no user-declared constructors (
Pod p{1, 2}; // Pod is an aggregate
Pod p{}; // default initialization - all fields are zeroed
Pod p{1}; // same as {1, 0}int array_1[]{ 1, 2, 3 }; // Array of length 3; 1, 2, 3
int array_2[5]{}; // Array of length 5; 0, 0, 0, 0, 0
int array_3[5]{ 1, 2, 3 }; // Array of length 5; 1, 2, 3, 0, 0
int array_4[5]; // Array of length 5; uninitialized values- since C++11
- initializes an object from braced-init-list (constructors, methods)
- prevents narrowing conversions
- first looks for
std::initializer_list(with matching type), then for constructor with matching arguments, or aggregates
Point p1{1, 1}; direct-list initialization
Point p1 = {1, 1}; copy-list initialization (doesn't really call copy constructor)
foo({1, 2, 3}); method call (method accepts std::initializer_list<int>)- assignment operator is called when an object is assigned a new value
- doesn't happen on object initialization, the variable must be already initialized
Point p1 = Point(1, 2); // not assignment, but copy-initialization
p1 = Point(3, 4); // assignment