c语言初始化程序
Initializer list is used to initialize data members. The syntax begins with a colon(:) and then each variable along with its value separated by a comma. The initializer list does not end in a semicolon.
初始化程序列表用于初始化数据成员。 语法以冒号(:)开头,然后是每个变量及其值,并以逗号分隔。 初始化列表不以分号结尾。
Syntax:
句法:
Constructorname(datatype value1, datatype value2):datamember(value1),datamember(value2)
{
...
}
For example:
例如:
#include<iostream>
using namespace std;
class Base
{
private:
int value;
public:
// default constructor
Base(int value):value(value)
{
cout << "Value is " << value;
}
};
int main()
{
Base il(10);
return 0;
}
Value is 10
值是10
The above code is just an example to understand the syntax of Initializer list. In the above code, value
can easily be initialized inside the constructor as well, hence we do not have to use initializer list.
上面的代码仅是了解Initializer列表语法的示例。 在上面的代码中, value
也可以很容易地在构造函数内部初始化,因此我们不必使用初始化列表。
C ++中初始化列表的使用 (Uses of Initializer List in C++)
There are situations where initialization of data members inside constructor doesn't work and Initializer List must be used. Following are such cases:
在某些情况下,构造函数内部的数据成员的初始化无法正常工作,必须使用初始化列表。 以下是这种情况:
1)没有基类默认构造函数时 (1) When no Base class default constructor is present)
In Inheritance base class constructor is called first(Order of Constructor call), followed by the child class constructor.
在继承中,基类的构造函数首先被调用( Order of Constructor call ),随后是子类的构造函数。
Therefore, in the example below Base_
class constructor will be called before InitilizerList_
class constructor due to which the below program will throw compilation error: "No default constructor exists for class Base_".
因此,在下面的例子中Base_
类的构造函数将之前被称为InitilizerList_
类的构造函数,由于其下面的程序将抛出编译错误:“没有默认构造函数存在类Base_”。
#include<iostream>
using namespace std;
class Base_
{
public:
// parameterized constructor
Base_(int x)
{
cout << "Base Class Constructor. Value is: " << x << endl;
}
};
class InitilizerList_:public Base_
{
public:
// default constructor
InitilizerList_()
{
Base_ b(10);
cout << "InitilizerList_'s Constructor" << endl;
}
};
int main()
{
InitilizerList_ il;
return 0;
}
The above code example can be rewritten using initializer list, and will execute smoothly without any error.
上面的代码示例可以使用初始化列表重写,并且可以顺利执行而没有任何错误。
Here is the new code:
这是新代码:
#include<iostream>
using namespace std;
class Base_
{
public:
// parameterized constructor
Base_(int x)
{
cout << "Base Class Constructor. Value is: " << x << endl;
}
};
class InitilizerList_:public Base_
{
public:
// default constructor using initializer list
InitilizerList_():Base_(10)
{
cout << "InitilizerList_'s Constructor" << endl;
}
};
int main()
{
InitilizerList_ il;
return 0;
}
Base Class Constructor value is 10
InitilizerList_'s constructor
基类构造函数的值为10 InitilizerList_的构造函数
2)使用引用类型时 (2) When reference type is used)
If you have a data member as reference type, you must initialize it in the initialization list. References are immutable hence they can be initialized only once.
如果您有数据成员作为引用类型,则必须在初始化列表中对其进行初始化。 引用是不可变的,因此只能初始化一次。
#include<iostream>
using namespace std;
class Base
{
private:
int &ref;
public:
Base(int &ref):ref(ref)
{
cout << "Value is " << ref;
}
};
int main()
{
int ref=10;
Base il(ref);
return 0;
}
Value is 10
值是10
3)用于初始化const
数据成员 (3) For initializing const
data member)
const
data members can be initialized only once, so it must be initialized in the initialization list.
const
数据成员只能初始化一次,因此必须在初始化列表中进行初始化。
#include<iostream>
using namespace std;
class Base
{
private:
const int c_var;
public:
Base(int c_var):c_var(c_var)
{
cout << "Value is " << c_var;
}
};
int main()
{
Base il(10);
}
Value is 10
值是10
4)当数据成员和参数具有相同的名称时 (4) When data member and parameter have same name)
#include<iostream>
using namespace std;
class Base
{
private:
int value;
public:
Base(int value):value(value)
{
cout << "Value is " << value;
}
};
int main()
{
Base il(10);
return 0;
}
Value is 10
值是10
5)为了提高性能 (5) For improving performance)
If you are assigning the values inside the body of the constructor, then a temporary object would be created which will be provided to the assignment operator. The temporary object will be destroyed at the end of the assignment statement. Creation of temporary object can be avoided by using initializer list.
如果要在构造函数的主体内分配值,则将创建一个临时对象,该对象将提供给赋值运算符。 临时对象将在赋值语句的末尾销毁。 通过使用初始化程序列表,可以避免创建临时对象。
翻译自: https://www.studytonight.com/cpp/initializer-list-in-cpp.php
c语言初始化程序