继承 public/protected/private 个构造函数
Inheriting public/protected/private constructors
如果我理解正确的话:
class Base { /*...*/ };
class Derived: public Base { public: using Base::Base; }
将强制继承 Derived
.
中的所有 Base
构造函数
但是 public/protected/private 构造函数呢?
class Base {
friend void g();
public:
Base (A a);
protected:
Base (B b);
private:
Base (C c);
};
class Derived: public Base {
public:
using Derived::Derived;
};
我找不到这方面的任何规范,但我尝试了以下方法:
void f () {
Derived{A{}}; // OK
Derived{B{}}; // Inaccessible
Derived{C{}}; // Inaccessible
}
void g () {
Derived{A{}}; // OK
Derived{B{}}; // OK
Derived{C{}}; // OK
}
所以看起来 using Base::Base
在决定继承哪些构造函数时没有考虑访问修饰符(它继承了私有构造函数)但是它继承了那些修饰符(private/protected其他人仍然无法访问)并且它允许 private/protected 访问 Base
的朋友(友谊不会继承,因此 g
不是 Derived
的朋友,但它仍然可以访问继承自 Base
).
的 Derived
的 private/protected 构造函数
这是正确且标准的行为吗?
您要查找的规格在 [namespace.udecl] ¶19,重点是我的。
A using-declarator that names a constructor does not create a synonym;
instead, the additional constructors are accessible if they would be accessible when used to construct an object of the corresponding base
class, and the accessibility of the using-declaration is ignored.
您的测试与该段一致。可访问性检查失败或通过,与在您检查的范围内构造 Base
时完全一样。
如果我理解正确的话:
class Base { /*...*/ };
class Derived: public Base { public: using Base::Base; }
将强制继承 Derived
.
Base
构造函数
但是 public/protected/private 构造函数呢?
class Base {
friend void g();
public:
Base (A a);
protected:
Base (B b);
private:
Base (C c);
};
class Derived: public Base {
public:
using Derived::Derived;
};
我找不到这方面的任何规范,但我尝试了以下方法:
void f () {
Derived{A{}}; // OK
Derived{B{}}; // Inaccessible
Derived{C{}}; // Inaccessible
}
void g () {
Derived{A{}}; // OK
Derived{B{}}; // OK
Derived{C{}}; // OK
}
所以看起来 using Base::Base
在决定继承哪些构造函数时没有考虑访问修饰符(它继承了私有构造函数)但是它继承了那些修饰符(private/protected其他人仍然无法访问)并且它允许 private/protected 访问 Base
的朋友(友谊不会继承,因此 g
不是 Derived
的朋友,但它仍然可以访问继承自 Base
).
Derived
的 private/protected 构造函数
这是正确且标准的行为吗?
您要查找的规格在 [namespace.udecl] ¶19,重点是我的。
A using-declarator that names a constructor does not create a synonym; instead, the additional constructors are accessible if they would be accessible when used to construct an object of the corresponding base class, and the accessibility of the using-declaration is ignored.
您的测试与该段一致。可访问性检查失败或通过,与在您检查的范围内构造 Base
时完全一样。