C++类的构造函数

一,关于构造函数

作用:初始化类的成员变量,所以当创建类对象或者类对象被创建就会调用构造函数

特点:

  1. 函数名和类名一样,且没有返回值
  2. 当没有显式的定义构造函数时,系统会自己生成默认的构造函数
  3. 构造函数可以重载

二,构造函数的几种表现形式

2.1 默认的构造函数

Test1.cpp

 1 #include "test1.h"
 2 
 3 Test1::Test1()
 4 {
 5     cout<< "Test1 construction" << endl;
 6 }
 7 
 8 Test1::~Test1()
 9 {
10     cout<< "~Test1 destructor" << endl;
11 }
12 
13 void Test1::getInfo()
14 {
15     cout << "get: year: " << m_year << " month: " << m_month << " day: " << m_day << endl;
16 }

Test1.h

#ifndef TEST1_H
#define TEST1_H
#include <iostream>
using namespace std;

class Test1
{
public:
    Test1();//默认构造函数
    ~Test1();//析构函数
    void getInfo();
private:
    int m_year;
    int m_month;
    int m_day;
};

#endif // TEST1_H

2.2 自定义的构造函数

Test2.cpp

 1 /*
 2  * Test2
 3 */
 4 Test2::Test2()
 5 {
 6     cout<< "Test2 default construction" << endl;
 7 }
 8 Test2::Test2(int year, int month, int day)
 9 {
10     m_year = year;
11     m_month = month;
12     m_day = day;
13     cout<< "Test2 construction" << endl;
14 }
15 
16 Test2::~Test2()
17 {
18     cout<< "~Test2 destructor" << endl;
19 }
20 
21 void Test2::getInfo()
22 {
23     cout << "get: year: " << m_year << " month: " << m_month << " day: " << m_day << endl;
24 }

Test2.h

 1 class Test2
 2 {
 3 public:
 4     Test2();//默认构造函数
 5     Test2(int year=19, int month=4, int day=15);//带有默认参数的构造函数
 6     ~Test2();//析构函数
 7     void getInfo();
 8 private:
 9     int m_year;
10     int m_month;
11     int m_day;
12 };

注意事项:

注意使用默认参数时,有可能出现重复定义的构造函数

 1 //测试用例1
 2 Test2 t21;//错误的使用,重复的构建函数
 3 t21.getInfo();
 4 
 5 //错误信息
 6 /*
 7 D:\QT_project\test\Test\main.cpp:21: error: C2668: 'Test2::Test2' : ambiguous call to overloaded function
 8 d:\qt_project\test\test\test1.h(23): could be 'Test2::Test2(int,int,int)'
 9 d:\qt_project\test\test\test1.h(22): or       'Test2::Test2(void)'
10 while trying to match the argument list '()'
11 */
12 
13 //测试用例2  
14 Test2 t22(1,2);//正确的用法
15 t22.getInfo();

2.3 初始化列表

注意事项1:

初始化列表的赋值时刻和函数体内赋值是不同的,初始化列表是在对象创建成功之前完成的,而函数体内赋值是在对象成员创建成功后进行赋值操作。

必须使用初始化列表的几种场景:

  1. 成员是const类型或者是引用类型
  2. 有一个成员是类类型的对象(且没有缺省的构造函数)

Test3.cpp

 1 /*
 2  * Test3
 3 */
 4 
 5 Test3::~Test3()
 6 {
 7     cout<< "~Test3 destructor" << endl;
 8 }
 9 
10 void Test3::getInfo()
11 {
12     cout << "get: year: " << m_year << " month: " << m_month << " day: " << m_day << endl;
13 }

Test3.h

 1 class Test3
 2 {
 3 public:
 4     //Test3();//默认构造函数  当前成员变量有const类型,且没有初始化,因此无法使用默认的构造函数
 5     Test3(int year, int month, int day)
 6         :m_year(year),m_month(month),m_day(day)//初始化列表
 7     {}
 8     Test3(int year=19, int month=4);
 9     ~Test3();//析构函数
10     void getInfo();
11 private:
12     int m_year;
13     int m_month;
14     int const m_day;
15 };

main.cpp

1 Test3 t3(19,4,15);
2 t3.getInfo();

关于1,因为对于const和引用类型必须要进行初始化,所以必须在初始化列表中进行初始化

关于2,当类类型成员有缺省的构造函数时,在创建对象的时候系统会默认调用,因为不用传参。但是当你的构造函数不是缺省的,如果不在初始化列表中进行调用构造函数,系统就无法知道怎么调用类类型成员的构造函数,那么就无法创建。

 1 class Time
 2 {
 3 public:
 4     Time(int h)
 5     {
 6         m_hour = h;
 7     }
 8 private:
 9     int m_hour;
10 };
11 
12 class Test3
13 {
14 public:
15     Test3(int year, int month, int day)
16         :m_year(year),m_month(month),m_day(day), t(10)//初始化列表
17     {}
18     ~Test3();//析构函数
19     void getInfo();
20 private:
21     int m_year;
22     int m_month;
23     int const m_day;
24     Time t;
25 };

注意事项2:

成员初始化顺序:初始化列表的顺序并不限定初始化的执行循序。成员的初始化顺序是与类中定义的的顺序保持一致。

<文档结束>

猜你喜欢

转载自www.cnblogs.com/yechaoy/p/10710379.html