类定义
主要涉及知识点:
- 同一类型的多个数据成员
- 使用类型别名来简化类
- 成员函数可被重载 - 定义重载成员函数
- 显式指定
inline
成员函数一、构造函数
在类中有一种特殊的成员函数,它的名字与类名相同,我们在创建类的时候,这个特殊的成员函数就会被系统调用。这个成员函数,就叫“构造函数”。
因为构造函数会被系统自动调动,构造函数的目的就是初始化类对象的数据成员。
注意:
(1)构造函数没有返回值,这是构造函数的特殊之处。
(2)不可以手工调用构造函数,否则编译会出错。
(3)正常情况下,构造函数应该被声明为public,因为创建一个对象时,系统要替我们调用构造函数,这说明构造函数是一个public的成员
类缺省的成员是私有成员,因此对于构造函数我们必须将其声明为public。二、C++类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
初始化和赋值对内置类型的成员没有什么大的区别。对非内置类型成员变量,为了避免两次构造,推荐使用类构造函数初始化列表。
但有的时候必须用带有初始化列表的构造函数:
1.成员类型是没有默认构造函数的类。若没有提供显示初始化式,则编译器隐式使用成员类型的默认构造函数,若类没有默认构造函数,则编译器尝试使用默认构造函数将会失败。
2.const
成员或引用类型的成员。因为const
对象或引用类型只能初始化,不能对他们赋值。
初始化数据成员与对数据成员赋值的含义是什么?有什么区别?
首先把数据成员按类型分类并分情况说明:
1.内置数据类型,复合类型(指针,引用)
在成员初始化列表和构造函数体内进行,在性能和结果上都是一样的
2.用户定义类型(类类型)
结果上相同,但是性能上存在很大的差别。因为类类型的数据成员对象在进入函数体前已经构造完成,也就是说在成员初始化列表处进行构造对象的工作,调用构造函数,在进入函数体之后,进行的是对已经构造好的类对象的赋值,又调用个拷贝赋值操作符才能完成(如果并未提供,则使用编译器提供的默认按成员赋值行为)
初始化列表的成员初始化顺序:
C++初始化类成员时,是按照声明的顺序初始化的,而不是按照出现在初始化列表中的顺序。
1 |
|
执行结果:
参考链接:
构造函数详解,explicit,初始化列表
C++ 类构造函数初始化列表
三、前向声明
前向声明&include
区别:
前向声明概念(forward declaration
):
在程序中引入了类类型的B.在声明之后,定义之前,类B是一个不完全类型(incompete type
),即已知B
是一个类型,但不知道包含哪些成员.不完全类型只能以有限方式使用,不能定义该类型的对象,不完全类型只能用于定义指向该类型的指针及引用,或者用于声明(而不是定义)使用该类型作为形参类型或返回类型的函数.
前向声明应用场景:
当你需要定义两个类或者结构,例如A和B,而这两个类需要互相引用,这时候在定义A的时候,B还没有定义,那怎么引用它呢,这时候就需要前向声明(forward declaration)了;
前向声明格式如下: class B;当你在定义类A之前声明了B,那么就会在程序中引入了类类型的B,编译器知道你会在后面的某个地方定义类B,所以允许你在类A中引用类B。
但是,在声明之后,定义之前,类B是一个不完全类型(incompete type),即已知B是一个类型,但不知道这个类型的一些性质(比如包含哪些成员和操作)。
前向声明的使用限制:(就拿上面声明B类说明)
1.不能够定义B类的对象;
2.可以用于定义只想这个类型的指针或者引用;
3.用于声明使用该类型作为形参或者返回类型的函数;
除了上面的限制,我们可以用它做些什么事情吗?
在C++中,如果要编写一个新类的头文件,一般是要#include一堆依赖的头文件,但利用前向声明和c++编译器的特性,可以减少这里的工作量。
因为c++编译器做的事情主要是:
1.扫描符号;
2.确定对象大小。
利用这个特性,当我们编写一个新类的头文件时,就可以用前向声明,减少大量的#include,减少编译的工作量。
1 |
|
执行结果:
参考链接:
前向声明&include区别