위임 생성자

많은 클래스에는 비슷한 작업을 수행하는 여러 생성자가 있습니다. 예를 들어 매개 변수의 유효성을 검사합니다.

class class_c {
public:
    int max;
    int min;
    int middle;

    class_c() {}
    class_c(int my_max) {
        max = my_max > 0 ? my_max : 10;
    }
    class_c(int my_max, int my_min) {
        max = my_max > 0 ? my_max : 10;
        min = my_min > 0 && my_min < max ? my_min : 1;
    }
    class_c(int my_max, int my_min, int my_middle) {
        max = my_max > 0 ? my_max : 10;
        min = my_min > 0 && my_min < max ? my_min : 1;
        middle = my_middle < max && my_middle > min ? my_middle : 5;
    }
};

모든 유효성 검사를 수행하는 함수를 추가하여 반복 코드를 줄일 수 있지만class_c, 한 생성자가 작업의 일부를 다른 생성자에게 위임할 수 있는지를 쉽게 이해하고 기본. 위임 생성자를 추가하려면 다음 구문을 사용합니다 constructor (. . .) : constructor (. . .) .

class class_c {
public:
    int max;
    int min;
    int middle;

    class_c(int my_max) {
        max = my_max > 0 ? my_max : 10;
    }
    class_c(int my_max, int my_min) : class_c(my_max) {
        min = my_min > 0 && my_min < max ? my_min : 1;
    }
    class_c(int my_max, int my_min, int my_middle) : class_c (my_max, my_min){
        middle = my_middle < max && my_middle > min ? my_middle : 5;
}
};
int main() {

    class_c c1{ 1, 3, 2 };
}

이전 예제를 단계별로 진행하면서 생성자가 먼저 생성자를 class_c(int, int, int)class_c(int, int)호출하고, 다시 호출 class_c(int)합니다. 각 생성자는 다른 생성자가 수행하지 않는 작업만 수행합니다.

호출되는 첫 번째 생성자는 해당 시점에서 모든 멤버가 초기화되도록 개체를 초기화합니다. 다음과 같이 다른 생성자에 위임하는 생성자에서는 멤버 초기화를 수행할 수 없습니다.

class class_a {
public:
    class_a() {}
    // member initialization here, no delegate
    class_a(string str) : m_string{ str } {}

    //can't do member initialization here
    // error C3511: a call to a delegating constructor shall be the only member-initializer
    class_a(string str, double dbl) : class_a(str) , m_double{ dbl } {}

    // only member assignment
    class_a(string str, double dbl) : class_a(str) { m_double = dbl; }
    double m_double{ 1.0 };
    string m_string;
};

다음 예제에서는 비정적 데이터 멤버 이니셜라이저의 사용을 보여 줍니다. 생성자도 지정된 데이터 멤버를 초기화하면 멤버 이니셜라이저가 재정의됩니다.

class class_a {
public:
    class_a() {}
    class_a(string str) : m_string{ str } {}
    class_a(string str, double dbl) : class_a(str) { m_double = dbl; }
    double m_double{ 1.0 };
    string m_string{ m_double < 10.0 ? "alpha" : "beta" };
};

int main() {
    class_a a{ "hello", 2.0 };  //expect a.m_double == 2.0, a.m_string == "hello"
    int y = 4;
}

생성자 위임 구문은 생성자 재귀를 실수로 만드는 것을 방지하지 않습니다. Constructor1은 Constructor1을 호출하는 Constructor2를 호출하며 스택 오버플로가 있을 때까지 오류가 throw되지 않습니다. 주기를 피하는 것은 사용자의 책임입니다.

class class_f{
public:
    int max;
    int min;

    // don't do this
    class_f() : class_f(6, 3){ }
    class_f(int my_max, int my_min) : class_f() { }
};