BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / soft-design / #16565同步于 2007/4/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖

问一个C++析构函数的问题

itrt
2007/4/10镜像同步5 回复
class a { public: a(){cout<<"a+"<<endl;} ~a(){cout<<"a-"<<endl;} }; class b:virtual public a { public: b(){cout<<"b+"<<endl;} ~b(){cout<<"b-"<<endl;} }; class c:virtual public a { public: c(){cout<<"c+"<<endl;} ~c(){cout<<"c-"<<endl;} }; class d: public c, public b { public: d(){cout<<"d+"<<endl;} ~d(){cout<<"d-"<<endl;} }; 调用d时,析构顺序是 a+c+b+d+ 为什么把d的定义改了一下如:class d: public c, virtual public b 顺序就变成了a+b+c+d+了呢?
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
PomLover机器人#1 · 2007/4/10
C++是先按顺序构造虚基类,再按顺序构造非虚基类~
StarFallLuna机器人#2 · 2007/4/11
好久不看忘记了,这记性..... Sigh... cpp standard 12.6.2-5 Initialization shall proceed in the following order: — First, and only for the constructor of the most derived class as described below, virtual base classes shall be initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base class names in the derived class base-specifier-list — Then, direct base classes shall be initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers). — Then, nonstatic data members shall be initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers). — Finally, the body of the constructor is executed. 【 在 PomLover (PomLover) 的大作中提到: 】 : C++是先按顺序构造虚基类,再按顺序构造非虚基类~
hman机器人#3 · 2007/4/12
没有在继承的时候用过virtual关键字。 不知道是啥意思哦。
commonplace机器人#4 · 2007/4/14
虚拟继承是为了提高效率用的
commonplace机器人#5 · 2007/4/14
RE 【 在 PomLover 的大作中提到: 】 : C++是先按顺序构造虚基类,再按顺序构造非虚基类~