다음을 통해 공유


배열 초기화

클래스 생성자가 있으면 배열은 해당 클래스의 생성자에 의해 초기화 됩니다.이니셜라이저 목록의 배열 요소를에서 보다 적게 항목이 있으면 나머지 요소에 대 한 기본 생성자 사용 됩니다.기본 생성자에 대 한 클래스 정의 되어 있는 경우 이니셜라이저 목록을 완료 해야-즉, 있어야 한 이니셜라이저는 배열의 각 요소에 대 한.

고려는 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()
{
}

참고 항목

참조

특수 멤버 함수를 사용 하 여 초기화