首页 > 编程语言 > 详细

C++继承中构造函数和析构函数的调用顺序

时间:2014-04-10 02:35:12      阅读:426      评论:0      收藏:0      [点我收藏+]
 
class Fish:public Animal
{
public:
Fish()
{
cout<<"this is Fish"<<endl;
}
~Fish()
{
cout<<"this is free Fish"<<endl;
}
};
C++继承中父类的构造函数先于子类的构造函数调用;

子类的析构函数的调用顺序先于父类的析构函数调用;

class Animal
{
public:
Animal()
{
cout<<"this is animal"<<endl;
}
~Animal()
{
cout<<"this is free animal"<<endl;
}
};

void main()

{

Fish fish;

}

如果父类中的构造函数含有两个参数,则子类中声明构造函数需要注意

如:

Animal中的构造函数是

Animal(int i,int j)

{}

则子类的构造函数:

Fish():Animal(100,200)

{}

 

上面两段代码先后顺序颠倒了............................................................................................

                                                                                                                                                                                                                                 ————郭仔




 

 

C++继承中构造函数和析构函数的调用顺序,布布扣,bubuko.com

C++继承中构造函数和析构函数的调用顺序

原文:http://blog.csdn.net/guoxianzhuang/article/details/23271961

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!