提问人:shane 提问时间:9/20/2017 最后编辑:shane 更新时间:9/20/2017 访问量:280
在受保护时使用基类构造函数
Using base class constructor when it is protected
问:
我想在子类中使用基类构造函数,基类构造函数受到保护,因此如果没有子类就无法实例化它。
class A {
protected:
A() {}
};
class B : public A {
public:
B() : A() {}
};
但是我不能在没有编译器抱怨受保护的情况下使用这样的 using 指令,即使 using 在公共块中也是如此。B::B()
class B {
public:
using A::A;
};
此行为在何处指定?
编辑
我试图简化这个例子,但没有检查它是否编译,显然它有一个微不足道的构造函数,但这段代码不能编译
class A {
protected:
A(int x) {}
};
class B : public A{
public:
using A::A;
};
int main() {
B b(4);
}
有错误
main.cpp: In function 'int main()':
main.cpp:12:8: error: 'A::A(int)' is protected within this context
B b(4);
^
main.cpp:3:5: note: declared protected here
A(int x) {}
^
exit status 1
答: 暂无答案
评论
B
A
B
A