LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 172|回复: 0

初始化数组

[复制链接]
发表于 2024-2-4 23:13:01 | 显示全部楼层 |阅读模式
具有类构造函数的对象数组由构造函数初始化。 如果初始化表达式列表中的项少于数组中的元素,则默认的构造函数将用于剩余元素。 如果没有为类定义默认构造函数,初始化表达式列表必须完整,即数组中的每个元素都必须有一个初始化表达式。

考虑定义了两个构造函数的Point 类:

// initializing_arrays1.cpp
class Point
{
public:
   Point()   // Default constructor.
   {
   }
   Point( int, int )   // Construct from two ints
   {
   }
};

// An array of Point objects can be declared as follows:
Point aPoint[3] = {
   Point( 3, 3 )     // Use int, int constructor.
};

int main()
{
}
aPoint 的第一个元素是使用构造函数 Point( int, int ) 构造的;剩余的两个元素是使用默认构造函数构造的。

静态成员数组(是否为 const)可在其定义中进行初始化(类声明的外部)。 例如:

// initializing_arrays2.cpp
class WindowColors
{
public:
    static const char *rgszWindowPartList[7];
};

const char *WindowColors::rgszWindowPartList[7] = {
    "Active Title Bar", "Inactive Title Bar", "Title Bar Text",
    "Menu Bar", "Menu Bar Text", "Window Background", "Frame"   };
int main()
{
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表