Initialization (programming): Difference between revisions

Content deleted Content added
Not how logic works in English
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 15:
and not to turn this page into a C++ primer
-->
<sourcesyntaxhighlight lang="c">
int i = 0;
int k[4] = {0, 1};
Line 21:
char ty[2] = 'f';
struct Point {int x; int y;} p = { .y = 13, .x = 7 };
</syntaxhighlight>
</source>
 
C++ examples:
<sourcesyntaxhighlight lang="cpp">
int i2(0);
int j[2] = {rand(), k[0]};
MyClass* xox = new MyClass(0, "zaza");
point q = {0, i + 1};
</syntaxhighlight>
</source>
 
===Initializer list===
In C++, a [[constructor (object-oriented programming)|constructor]] of a class/struct can have an '''initializer list''' within the definition but prior to the constructor body. It is important to note that when you use an initialization list, the values are not assigned to the variable. They are initialized. In the below example, 0 is initialized into re and im.
Example:
<sourcesyntaxhighlight lang="c">
struct IntComplex {
IntComplex() : re(0), im(0) {}
Line 41:
int im;
};
</syntaxhighlight>
</source>
Here, the construct <code> : re(0), im(0)</code> is the initializer list.