0

Could you explain how does object definition bounds with the variable initialization, denoting initialization.

#include <iostream>
int a = 5; //definition

int main{ std::cout << a; }

sec. 1.8/1:

An object is created by a definition,[...]

a is statically initialized to 5. Initialization occurs as a part of an object definition, or it is independent from definition?

2 Answers2

0

From §7/8:

A definition causes the appropriate amount of storage to be reserved and any appropriate initialization (8.5) to be done.

So yes, a definition causes initialization.

However, initialization (and object creation in general) can be caused without a definition, which is described by the omitted part of your quote (§1.8/1):

An object is created by a definition (3.1), by a new-expression (5.3.4) or by the implementation (12.2) when needed.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

An object's life time begins after its initialization has completed. It is not possible to define an object without initializing it. Even if you say int a;, you define and initialize a, although this particular kind of initialization (called "default-initialization") does nothing and leaves the object a uninitialized.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084