C++中析构函数没有被执行?
请大家看下这段代码:
#include<iostream>
using namespace std;
class Student
{
public:
Student()
{
cout<<"constructing student."<<endl;
semesHours=100;
gpa=3.5;
}
~Student()
{
cout<<"destructing student."<<endl;
}
protected:
int semesHours;
float gpa;
};
class Teacher
{
public:
Teacher()
{
cout<<"constructing teacher."<<endl;
}
~Teacher()
{
cout<<"destructing teacher."<<endl;
}
};
class TutorPair
{
public:
TutorPair()
{
cout<<"constructing tutorPair."<<endl;
noMeetings=0;
}
~TutorPair()
{
cout<<"destructing tutorpair."<<endl;
}
protected:
Student student;
Teacher teacher;
int noMeetings;
};
int main()
{
TutorPair tp;
cout<<"back in main."<<endl;
while(1);
return 0;
}
我在VS2005中运行的结果是:
constructing student.
constructing teacher.
constructing tutorPair.
back in main.
为什么析构函数没有被执行呢?
#include<iostream>
using namespace std;
class Student
{
public:
Student()
{
cout<<"constructing student."<<endl;
semesHours=100;
gpa=3.5;
}
~Student()
{
cout<<"destructing student."<<endl;
}
protected:
int semesHours;
float gpa;
};
class Teacher
{
public:
Teacher()
{
cout<<"constructing teacher."<<endl;
}
~Teacher()
{
cout<<"destructing teacher."<<endl;
}
};
class TutorPair
{
public:
TutorPair()
{
cout<<"constructing tutorPair."<<endl;
noMeetings=0;
}
~TutorPair()
{
cout<<"destructing tutorpair."<<endl;
}
protected:
Student student;
Teacher teacher;
int noMeetings;
};
int main()
{
TutorPair tp;
cout<<"back in main."<<endl;
while(1);
return 0;
}
我在VS2005中运行的结果是:
constructing student.
constructing teacher.
constructing tutorPair.
back in main.
为什么析构函数没有被执行呢?
...全文
相关推荐
#NAME?_CPP·CC
CC++ 核心编程帮助文档,很有用很经典的东东。很多人看他开始的编程学习。确实很好
C++之析构函数探究
析构函数 创建对象时系统会自动调用构造函数进行初始化工作,同样,销毁对象时系统也会自动调用一个函数来进行清理工作(回收对象占用的内存空间),这个函数被称为析构函数。 析构函数(Destructor)也是一种特殊的成员函数,没有返回值,不需要用户调用,而是在销毁对象时自动执行。与构造函数不同的是,析构函数的名字是在类名前面加一个“∼\sim∼”符号。e.g: ~Test( ); 析构函数的四个特点: 一. 没有返回值; 二. 没有参数; 三. 不能被重载(唯一); 四. 必须公有.
C++中确定基类有虚析构函数
本文给大家介绍了C++中确定基类有虚析构函数的方法。
详解C++纯虚函数与抽象类
主要介绍了C++纯虚函数与抽象类的相关资料,帮助大家更好的理解和学习c++,感兴趣的朋友可以了解下
C++程序员容易犯的十个C#错误
你是一个老程序员么?如果你是一个C++程序员,现在开始写C#了,那么一定要看这个!


