Visual C++ 析构函数对虚基类的访问存在 BUG





2.00/5 (1投票)
2000年4月6日

63146

1
当继承自虚基类时,析构函数的访问说明符将被忽略
引言
此错误出现在 VC6 SP3 中,也可能出现在更早的版本中。
当继承自虚基类时,析构函数的访问说明符将被忽略
为了演示,如果像下面所示在类 B 的声明中注释掉单词 virtual,则该程序将正确生成错误消息
xxx.cpp(28) : error C2248: 'B::~B' : cannot access protected member declared in class 'B' xxx.cpp(23) : see declaration of 'B::~B'
如果取消注释单词 virtual,则程序将在析构函数声明为私有函数的情况下也能成功编译,不会产生任何错误消息
有问题的代码
#include "stdafx.h" class A { protected: ~A() {} }; class B : virtual public A { private: ~B() {} }; int main(int argc, char* argv[]) { B b; return 0; }
作为一些背景信息,我正在实现一些智能指针,并希望确保智能指针的目标对象不能在堆栈上局部创建,也不希望能够显式调用指向此类对象的 delete。 因此,我希望析构函数是私有的。 但是,当我从虚基类继承时,这会失败(如上所示)。