Content deleted Content added
Not how logic works in English |
No edit summary |
||
(14 intermediate revisions by 11 users not shown) | |||
Line 1:
{{Short description|Assignment of an initial value for variable}}
In [[computer programming]], '''initialization'''
Initialization is done either by statically embedding the value at compile time, or else by assignment at [[Run time (program lifecycle phase)|run time]]. A section of code that performs such initialization is generally known as "initialization code" and may include other, one-time-only, functions such as opening files; in [[object-oriented programming]], initialization code may be part of a ''[[Constructor (object-oriented programming)|constructor]]'' (class method) or an ''initializer'' (instance method). Setting a memory ___location to [[hexadecimal]] zeroes is also sometimes known as "clearing" and is often performed by an [[exclusive or]] instruction (both operands specifying the same variable), at [[machine code]] level, since it requires no additional memory access.
Line 15 ⟶ 16:
and not to turn this page into a C++ primer
-->
<
int i = 0;
int k[4] = {0, 1};
Line 21 ⟶ 22:
char ty[2] = 'f';
struct Point {int x; int y;} p = { .y = 13, .x = 7 };
</syntaxhighlight>
C++ examples:
<
int i2(0);
int j[2] = {rand(), k[0]};
MyClass* xox = new MyClass(0, "zaza");
point q = {0, i + 1};
</syntaxhighlight>
===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:
<
struct IntComplex {
IntComplex() : re(0), im(0) {}
Line 41 ⟶ 42:
int im;
};
</syntaxhighlight>
Here, the construct <code> : re(0), im(0)</code> is the initializer list.
Sometimes the term "initializer list" is also used to refer to the list of expressions in the array or struct initializer.
[[C++11]] provides for a [[C++11#Initializer_lists|more powerful concept of initializer lists]], by means of a template, called
===Default initialization===
|