2014-01-09 4 views
28

:C++ (11) 다음과 같은 레이아웃 가정 상속 생성자 및 액세스 수정

class Base 
{ 
protected: 
    Base(P1 p1, P2 p2, P3 p3); 

public: 
    virtual void SomeMethod() = 0; 
} 

class Derived : public Base 
{ 
public: 
    using Base::Base; 

public: 
    virtual void SomeMethod() override; 
}; 

내가 여기 public으로 Derived의 생성자를 지정 할 수 있어야한다? VC++에서 다음과 같은 오류가 발생합니다 :

cannot access protected member declared in class 'Derived'
compiler has generated 'Derived::Derived' here [points to the using Base::Base line]
see declaration of 'Derived'

즉, 상속 된 생성자 위의 액세스 한정자는 무시됩니다.

이 기능의 제한 사항입니까? Base 클래스가 (순수 가상 메서드로 인해) 직접적으로 인스턴스화 될 수 없으므로 공용 생성자를 가지는 것은 의미가 없습니다. 12.9/4에 따르면

답변

23

, "상속 생성자"using X::X 말,

A constructor so declared has the same access as the corresponding constructor in X.

그래서 상속 생성자는 protected입니다.

+0

[GCC 4.8의 올바른 예] (http://ideone.com/7JpC2J) –

+3

생성자에 관해서 특별한 점이 있습니까? 똑같은 기능이 일반 기능에는 적용되지 않습니다 ... – Mehrdad

+1

@TemplateRex : 왜 그런지 묻습니다. – Mehrdad