现在,G ++和VC++ 2010都不支持继承构造函数。为什么继承构造函数不被所有现有的C++编译器支持?
不过,我认为这是C++ 0x中最漂亮的功能之一。我认为编译器应该很容易实现。
为什么编译器对此功能不感兴趣?
假设我想通过继承的std :: string这样的设计我自己的字符串类:
class MyString : public std::string
{
public:
// I have to redefine many overloaded ctors here and forward their arguments to
// std::string's ctors. How tedious it will be!!!
};
一个美丽的代码示例:
struct B1
{
B1(char);
};
struct B2
{
B2(double);
B2(int);
};
struct D1 : B1, B2
{
using B1::B1; // D1(char)
using B2::B2; // D1(double), D1(int)
};
D1 d('c'); //OK, invokes D1(char)
“漂亮的代码示例”的问题:D1的B2部分如何初始化? – UncleBens 2010-12-11 17:17:13
@UncleBens,我认为它应该被默认初始化或者没有初始化。 – xmllmx 2010-12-11 17:19:23
'std :: string'并不意味着从(或任何其他STL容器)派生而来。 – fredoverflow 2010-12-11 17:19:44