问题一:析构函数的调用顺序
单个对象创建时构造函数的调用顺序
- 调用父类的构造函数
- 调用成员变量的构造函数(调用顺序与声明顺序相同)
- 调用类自身的构造函数
结论:析构函数与对应的构造函数调用顺序相反)
多个对象析构时
- 析构函数与对应的构造函数调用顺序相反)
实验分析:
#include <iostream>
using namespace std;
class Cat
{
const char* catName;
public:
Cat(const char* s)
{
catName = s;
cout << "Cat()" << catName << endl;
}
~Cat()
{
cout << "~Cat()" << catName << endl;
}
};
class Test
{
Cat A;
Cat B;
public:
Test() :A("AA"), B("BB")
{
cout << "Test()" << endl;
}
~Test()
{
cout << "`Test()" << endl;
}
};
Cat C("CC");
int main()
{
Test t;
//构造顺序:C A B
//析构顺序:B A C
return 0;
}
结果:与预想的一致
对于栈对象
的全局变量,类似与出栈与入栈的顺序,最后构造的对象最先被析构,堆对象
的析构发生在使用delete
时,与delete
的使用顺序有关。
问题2:const是否可以修饰对象,如果可以有什么特性
- const修饰的对象为只读对象
- 只读对象的成员变量不允许被改变
- 只读对象只是编译阶段的概念,运行时无效
- const对象只能调用const成员函数
- const成员函数只能调用const成员函数
- const成员函数不可以修改成员变量的值
const成员函数的定义:Type ClassName::function(Type p)const
成员函数声明之后,函数体之前类中的函数声明与实际函数定义中都必须带const关键字
测试:const对象修改成员函数,调用普通成员函数错误
#include <iostream>
using namespace std;
class Test
{
int mi;
public:
int mj;
Test(int i);
Test(const Test& t);
int getMi();
};
Test::Test(int i)
{
mi = i;
}
Test::Test(const Test& t)
{
}
int Test::getMi()
{
return mi;
}
int main()
{
const Test t(1);
cout << t.getMi() << endl;
t.mj = 10;
return 0;
}
···
结果:
改进:在getMi()函数后面加const
int getMi() const
{
return mi;
}
const成员函数修改成员变量报错:
类成员
成员函数和成员变量都属于具体对象的吗
由于代码段不可以动态的添加或者删除,所以所有的对象只能调用共享同一套成员函数,那么编译器是怎么区别是不同对象的成员函数呢?
#include <stdio.h>
class Test
{
int mi;
public:
int mj;
Test(int i);
Test(const Test& t);
int getMi();
void print();
};
Test::Test(int i)
{
mi = i;
}
Test::Test(const Test& t)
{
mi = t.mi;
}
int Test::getMi()
{
return mi;
}
void Test::print()
{
printf("this = %p\n", this);
}
int main()
{
Test t1(1);
Test t2(2);
Test t3(3);
printf("t1.getMi() = %d\n", t1.getMi());
printf("&t1 = %p\n", &t1);
t1.print();
printf("t2.getMi() = %d\n", t2.getMi());
printf("&t2 = %p\n", &t2);
t2.print();
printf("t3.getMi() = %d\n", t3.getMi());
printf("&t3 = %p\n", &t3);
t3.print();
return 0;
}
结果:
t1.getMi() = 1
&t1 = 008FFC40
this = 008FFC40
t2.getMi() = 2
&t2 = 008FFC30
this = 008FFC30
t3.getMi() = 3
&t3 = 008FFC20
this = 008FFC20
小结:
- 对象的析构对象与构造函数相反
- const关键字修饰对象得到只读对象
- 只读对象只能调用const成员函数
- 所有对象共享类的成员函数
- 隐藏的this指针用于表示当前对象