In computer programming, initialization or initialisation is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on the programming language, as well as the type, storage class, etc., of an object to be initialized. Programming constructs which perform initialization are typically called initializers and initializer lists. Initialization is distinct from (and preceded by) declaration, although the two can sometimes be conflated in practice. The complement of initialization is finalization, which is primarily used for objects, but not variables.
Initialization is done either by statically embedding the value at compile time, or else by assignment at 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 (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.
In C/C99/C++, an initializer is an optional part of a declarator. It consists of the '=' character followed by an expression or a comma-separated list of expressions placed in curly brackets (braces). The latter list is sometimes called the "initializer list" or "initialization list" (although the term "initializer list" is formally reserved for initialization of class/struct members in C++; see below). A declaration which creates a data object, instead of merely describing its existence, is commonly called a definition.
多くの人は、「宣言」と「定義」という用語を区別するのが便利だと感じており、よく見かける「宣言と定義の区別…」という表現では、宣言は単にデータ オブジェクト (または関数) を指定するものだと解釈されています。実際には、C++ 標準によれば、定義は宣言です。それでも、「宣言と定義」という用法は、形式的には誤りですが、よく使われています。 [ 1 ]すべての定義は宣言ですが、すべての宣言が定義であるとは限りません。
C言語の例:
int i = 0 ; int k [ 4 ] = { 0 , 1 }; char tx [ 3 ] = 'a' ; char ty [ 2 ] = 'f' ; struct Point { int x ; int y ; } p = { . y = 13 , . x = 7 };C++の例:
int i2 = 0 ; int i3 ( 0 ); int i4 { 0 }; int j [ 2 ] = { rand (), k [ 0 ]}; MyClass * xox = new MyClass ( 0 , "zaza" ); Point q {. x = 0 , . y = i + 1 };C++ では、クラス/構造体のコンストラクタは、定義内、コンストラクタ本体の前にメンバ初期化リスト(MIL) を持つことができます。初期化リストを使用すると、値は変数に代入されるのではなく、初期化されます。以下の例では、reとがimコンストラクタパラメータで初期化されます。例:
class GaussianInteger { private : int re ; int im ; public : explicit GaussianInteger ( int re = 0 , int im = 0 ) : re { re }, im { im } {} };ここでいう構成要素とは、re{re}, im{im}初期化子リストのことである。
場合によっては、「初期化子リスト」という用語は、配列または構造体初期化子内の式のリストを指すためにも使用されます。
C++11 では、テンプレートと呼ばれるものによって、より強力な初期化子リストの概念が提供されていますstd::initializer_list<T>。
メンバー初期化リストの初期化中に例外がスローされた場合、コンストラクタ本体内のtry/ブロックではキャッチできません。代わりに、「関数 try ブロック」を使用する必要があります。[ 2 ]catch
import std ;std :: exceptionを使用します。class Foo { private : Bar bar ; public : explicit Foo ( const Bar & b ) try : bar { b } { std :: println ( "Successfully construct Foo" ) ; } catch ( const exception & e ) { std :: println ( "Failed to construct Foo: {}" , e.what ( )); } };プログラム内で明示的な構文を用いなくても、データの初期化が行われる場合があります。例えば、静的変数が初期化子なしで宣言されている場合、プリミティブデータ型の変数は対応する型のゼロで初期化され、クラス型の静的オブジェクトはデフォルトコンストラクタで初期化されます。