“使用"调用基类构造函数的关键字 [英] The "Using" keyword to call base class constructor
问题描述
我有以下基类
class Grammateas
{
public:
Grammateas(std::string name):_name(name){};
virtual ~Grammateas(){};
private:
std::string _name;
};
以及下面的派生类
class Boithos final : public Grammateas
{
public:
//using Grammateas::Grammateas;
Boithos(int hours):Grammateas("das"),_hours(hours){};
virtual ~Boithos(){};
private:
int _hours;
};
我想使用基类构造函数来创建这样的对象
I want to use the Base class constructor to create object like this
Boithos Giorgakis(5); //works
Boithos Giorgakis("something"); //Bug
我读到我可以使用 using 关键字,但是当我尝试使用它时
I read that I can use the using keyword but when I try to use it like
using Grammateas::Grammateas;
编译器返回消息
错误:'Grammateas::Grammateas' 命名构造函数
error: ‘Grammateas::Grammateas’ names constructor
你能帮我理解带构造函数的 using 关键字吗?
Can you help me understand the using keyword with constructors?
推荐答案
您的代码 - 带有 using Grammateas::Grammateas;
未注释 - 应该可以工作.(但要注意:继承的构造函数会使 _hours
未初始化.)
Your code - with the using Grammateas::Grammateas;
uncommented - should work. (But beware: the inherited constructor would leave _hours
uninitialized.)
通过using
-declarations 继承构造函数是C++11 中的一个新特性.也许你的编译器还不支持这个特性,或者在结合继承的构造函数和其他重载时有问题.(如果它接受 final
说明符,它似乎首先被正确设置为编译 C++11.)
Inheriting constructors through using
-declarations is a new feature in C++11. Maybe your compiler does not yet support this feature or has problems combining inherited constructors and other overloads. (If it accepts the final
specifier, it appears to be set up correctly to compile C++11 in the first place.)
这篇关于“使用"调用基类构造函数的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!