상속 관계에서 호출할 부모 생성자를 지정하는 방법

#include <iostream>

class CParent {
public:

    CParent()
    {
        std::cout << "Parent\n";
    }

    CParent(const char* comment)
    {
        std::cout << comment;
    }
};

class CChild :private CParent {
public:

    CChild()
    {
        std::cout << "Child\n";
    }

    CChild(const char* comment)
        :CParent(comment)
    {
    }

};

int main()
{
    CChild child("Hello");

    return 1;
}

 

  • 위와같이 상속 관계에서 자식 생성자에서 부모 클래스의 어떤 생성자를 호출할지를 이니셜라이저를 통해 지정할 수 있습니다. 만약, 지정하지 않는 다면은 인자를 아무것도 받지 않는 부모 생성자가 호출됩니다.




+ Recent posts