钱能C++ P280~P283
#include <iostream.h>
#include <string.h>
class StudentID
{
public:
StudentID(int id = 0)
{
value = id;
cout << "StudentID 构造时 id " << value << endl;
}
~StudentID()
{
cout << "StudentID 析构时 id " << value << endl;
}
int value;
};
class Student
{
public:
Student(char *pName="noName", int ssID = 0)
{
cout << "Student 构造时 pName " << pName << endl;
strcpy(name,pName);
name[sizeof(name) - 1] = '\0';
StudentID id(ssID);
}
~Student()
{
cout << "Student 析构" << endl;
}
char name[20];
StudentID id; // 从输出可以看出,StudentID初始化了两次
// 首先初始化这里,再到构造方法
};
int main()
{
Student s("Tom",1988);
return 0;
}
StudentID 构造时 id 0
Student 构造时 pName Tom
StudentID 构造时 id 1988
StudentID 析构时 id 1988
Student 析构
StudentID 析构时 id 0