文章目录
C++中的对象组合
C++中的类可以使用其它类定义成员变量
问题:如何给对象成员进行初始化?
对象组合示例
#include <stdio.h>
class M
{
private:
int mI;
public:
M(int i)
{
printf("M(int i), i = %d\n", i);
mI = i;
}
int getI()
{
return mI;
}
};
class Test
{
private:
const int c;
M m1;
M m2;
public:
Test()
{
printf("Test()\n");
}
};
int main()
{
Test t1;
printf("Press any key to continue...");
getchar();
return 0;
}
分析:编译错误,那么如何给对象成员进行初始化?
C++中提供了初始化列表对成员变量进行初始化
语法规则
Constructor::Contructor() : m1(v1), m2(v1,v2), m3(v3)
{
// some other assignment operation
}
#include <stdio.h>
class M
{
private:
int mI;
public:
M(int i)
{
printf("M(int i), i = %d\n", i);
mI = i;
}
int getI()
{
return mI;
}
};
class Test
{
private:
const int c;
M m1;
M m2;
public:
Test() : c(1), m2(3), m1(2) //成员变量的初始化顺序与声明的顺序相关,与在初始化列表中的顺序无关. 所以仍然是m1的构造函数先调用
{
printf("Test()\n");
}
void print()
{
printf("c = %d, m1.mI = %d, m2.mI = %d\n", c, m1.getI(), m2.getI());
}
};
void run()
{
Test t1;
t1.print();
}
int main()
{
run();
printf("Press any key to continue...");
getchar();
return 0;
}
注意:
1.成员变量的初始化顺序与声明的顺序相关,与在初始化列表中的顺序无关
2.初始化列表先于构造函数的函数体执行
小插曲 类中的const成员
类中的const成员是肯定会被分配空间的
类中的const成员变量只是一个只读变量
注意:
编译器无法直接得到const成员变量的初始值,因此无法进入符号表成为真正意义上的常量。
注意:如果在const定义处可以看到它的值,则为真正意义上的常量,进入符号表;
否则为只读变量;
#include <stdio.h>
class M
{
private:
int mI;
public:
M(int i)
{
printf("M(int i), i = %d\n", i);
mI = i;
}
int getI()
{
return mI;
}
};
class Test
{
private:
const int c;
M m1;
M m2;
public:
Test() : c(1), m2(3), m1(2)
{
//尝试用指针去修改c的值,结果修改成功
const int* pp = &c;
int* ppp = const_cast<int*>(pp);
*ppp = 111;
printf("Test()\n");
}
void print()
{
printf("c = %d, m1.mI = %d, m2.mI = %d\n", c, m1.getI(), m2.getI());
}
};
void run()
{
Test t1;
t1.print();
}
int main()
{
run();
printf("Press any key to continue...");
getchar();
return 0;
}
分析:类中的const int c; 为只读变量;
初始化与赋值不同
初始化是用已存在的对象或值对正在创建的对象进行初值设置
赋值是用已存在的对象或值对已经存在的对象进行值设置
区别:
初始化:被初始化的对象正在创建
赋值:被赋值的对象已经存在
Test() : c(1) //此处c为初始化
{
c = 2; //此处c为赋值
}
对象的销毁
生活中存在的对象都是被初始化后才上市的
生活中的对象被销毁前会做一些清理工作
问题:如何清理被销毁的对象?
一般的解决方案
一般而言所有被销毁的对象都需要做清理
解决方案
为每个类都提供一个public的destroy函数
对象不再被需要时立即调用destroy函数进行清理
例如我们上一个例子中的destory()函数:
a1.destory();
a2.destory();
destroy只是一个普通的函数,必须显示的调用
如果对象销毁前没有做清理,那么很可能造成资源泄漏
在构造函数中申请的资源,需要在对象销毁前释放
思考:C++编译器是否能够自动调用某个特殊的函数进行对象的清理?
C++中的析构函数
C++中的类可以定义一个特殊的成员函数清理对象
这个特殊的成员函数叫做析构函数
定义:~ClassName()
析构函数没有参数也没有任何返回类型的声明
析构函数在对象销毁时自动被调用
#include <stdio.h>
class Test
{
private:
int mI;
public:
Test(int i) : mI(i)
{
printf("Test(), mI = %d\n", mI);
}
~Test()
{
printf("~Test(), mI = %d\n", mI);
}
};
void run()
{
Test t1(1);
Test t2(2);
Test t3(3);
Test t4(4);
Test t5(5);
}
int main()
{
run();
printf("Press any key to continue...");
getchar();
return 0;
}
Array类的进化
注意:析构函数不能重载
Array.cpp
#include "Array.h"
Array::Array(int length)
{
if( length < 0 )
{
length = 0;
}
mLength = length;
mSpace = new int[mLength];
}
Array::Array(const Array& obj)
{
mLength = obj.mLength;
mSpace = new int[mLength];
for(int i=0; i<mLength; i++)
{
mSpace[i] = obj.mSpace[i];
}
}
int Array::length()
{
return mLength;
}
void Array::setData(int index, int value)
{
mSpace[index] = value;
}
int Array::getData(int index)
{
return mSpace[index];
}
Array::~Array() //析构函数
{
printf("Array::~Array() \n");
mLength = -1;
delete[] mSpace;
}
Array.h
#ifndef _ARRAY_H_
#define _ARRAY_H_
class Array
{
private:
int mLength;
int* mSpace;
public:
Array(int length);
Array(const Array& obj);
int length();
void setData(int index, int value);
int getData(int index);
~Array(); //析构函数
};
#endif
main.cpp
#include <stdio.h>
#include "Array.h"
int main()
{
Array a1(10);
for(int i=0; i<a1.length(); i++)
{
a1.setData(i, i);
}
for(int i=0; i<a1.length(); i++)
{
printf("Element %d: %d\n", i, a1.getData(i));
}
Array a2 = a1;
for(int i=0; i<a2.length(); i++)
{
printf("Element %d: %d\n", i, a2.getData(i));
}
printf("Press any key to continue...");
getchar();
return 0;
}
构造函数与析构函数的调用秩序
当类中有成员变量是其它类的对象时
首先调用成员变量的构造函数
调用顺序与声明顺序相同
之后调用自身类的构造函数
析构函数的调用秩序与对应的构造函数调用秩序相反
析构函数的调用秩序与对应的构造函数调用秩序相反
#include <stdio.h>
class Test
{
private:
int mI;
public:
Test()
{
printf("Test()\n");
mI = -1;
}
Test(int i)
{
printf("Test(int i), i = %d\n", i);
mI = i;
}
Test(const Test& obj)
{
printf("Test(const Test& obj), i = %d\n", obj.mI);
mI = obj.mI;
}
~Test()
{
printf("~Test(), i = %d\n", mI);
}
};
void func(Test t)
{
Test r(1);
}
void run()
{
Test t(0);
func(t);
}
int main()
{
run();
printf("Press any key to continue...");
getchar();
return 0;
}
小结:
析构函数是C++中对象销毁时做清理工作的特殊函数
析构函数在对象销毁时自动被调用
析构函数是对象所使用的资源及时释放的保障
析构函数的调用秩序与构造函数相反
类的内部可以直接调用类的构造函数吗?
如果可以,直接调用构造函数会有什么情况发生呢?
可以直接调用构造函数,直接调用构造函数将得到一个临时对象。
#include <stdio.h>
class Test
{
private:
int mI;
int mJ;
const char* mS;
public:
Test()
{
printf("Test()\n");
mI = 0;
mJ = 0;
}
Test(const char* s)
{
printf("Test(const char* s)\n");
Test();
mS = s;
}
~Test()
{
printf("~Test()\n");
}
void print()
{
printf("mI = %d, mJ = %d, mS = %s\n", mI, mJ, mS);
}
};
void run()
{
Test t = Test("Delphi Tang"); // Test t("Delphi Tang");
t.print();
}
int main()
{
run();
printf("Press any key to continue...");
getchar();
return 0;
}
分析:mI和mJ并没有赋值为0;