C++ 虚拟基类初始化难题
我在静态库中有以下构造(作为调度程序机制的一部分,为了简洁起见删除了不相关的细节):C++ 虚拟基类初始化难题,c++,visual-c++,virtual,C++,Visual C++,Virtual,我在静态库中有以下构造(作为调度程序机制的一部分,为了简洁起见删除了不相关的细节): 类基{ 受保护的: Base(Connection*conn=nullptr):myConn(conn){;}/=nullptr修复了这个问题 连接*myConn; }; 类处理程序:虚拟公共基{ 公众: 虚空handleMessage(InputStream&)=0; 受保护的: Handler(intid):myId(id){;}这个问题在clang3.4中得到了修复,但在更早的版本中没有 一个编译器错误或
类基{
受保护的:
Base(Connection*conn=nullptr):myConn(conn){;}/=nullptr修复了这个问题
连接*myConn;
};
类处理程序:虚拟公共基{
公众:
虚空handleMessage(InputStream&)=0;
受保护的:
Handler(intid):myId(id){;}这个问题在clang3.4中得到了修复,但在更早的版本中没有
一个编译器错误或者至少是一个有争议的编译器特性,以及
Base(连接和连接)
受保护,您可以使用
面对面条件编译解决方案,例如
class Base {
protected:
#if HAVE_BUG_GCC19249
Base(Connection& conn = _workaround_bug_gcc19249_) : myConn(conn) {;}
#else
Base(Connection& conn) : myConn(conn) {;}
#endif
Connection& myConn;
#if HAVE_BUG_GCC19249
static Connection _workaround_bug_gcc19249_;
// ... conditional definition in implementation file.
#endif
};
根据,它感觉应该可以工作(Handler
是抽象的),但事实并非如此。值得一提的是,GCC也拒绝了这段代码。GCC bug。谢谢@Igor,真令人失望。但GCC bug真的很老了,在这方面没有任何进展吗?是的,或者像我在编辑的帖子中所做的那样,选择默认为nullptr的Connection*。
class A : public TypedHandler<SuperEvent>
, public TypedHandler<DuperEvent>
{
public:
A(Connection* conn) : Base(conn) {}
void onEvent(const SuperEvent&)
{ ... }
void onEvent(const DuperEvent&)
{ ... }
};
class Base {
protected:
#if HAVE_BUG_GCC19249
Base(Connection& conn = _workaround_bug_gcc19249_) : myConn(conn) {;}
#else
Base(Connection& conn) : myConn(conn) {;}
#endif
Connection& myConn;
#if HAVE_BUG_GCC19249
static Connection _workaround_bug_gcc19249_;
// ... conditional definition in implementation file.
#endif
};