使用using声明来显式地继承构造函数
下面是一个代码示例:
#include
template
class Base {
public:
Base(int);
};
template
Base::Base(int i) {
std::cout << "Base constructor called with " << i << std::endl;
}
template
class Derived : public Base {
public:
using Base::Base;
void foo() {
std::cout << "foo called" << std::endl;
}
};
int main() {
Derived d(42);
d.foo();
}
在此示例中,我们定义了一个基类“Base”,该类有一个接受int参数的构造函数。然后,我们定义了一个派生类“Derived”,该类公开继承了“Base”的构造函数,并定义了一个“foo”函数。
在主函数中,我们创建了一个“Derived”对象,并传递了一个值为42的int参数。我们随后调用“foo”函数。
通过使用“using Base