构造函数(初始化列表)
之前我们实现构造函数时,初始化成员变量都是在函数体内赋值,构造函数中初始化变量还有一种方法———初始化列表。初始化列表以冒号开始将要初始化的变量用逗号隔开。
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Date
{
public:
Date(int& x, int year = 1, int month = 1, int day = 1)
:_year(2024)
,_month(5)
,_day(10)
{
}
void Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
int i = 0;
Date d1(i);
d1.Print();
return 0;
}
在使用初始化列表时,要注意:
每个成员在初始化列表只能出现一次,在语法理解上初始化列表是每一个成员变量定义初始化的地方。
引用成员变量,const成员变量,没有默认构造的类类型变量,必须放在初始化列表进行初始化,不然编译会报错。
const类型成员必须在定义的时候就初始化,定义了之后不能改变
注意这是错误演示 ,const 类型成员要在初始化列表中初始化。
//注意这是错误演示
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Date
{
public:
Date(int& x, int year = 1, int month = 1, int day = 1)
:_year(2024)
,_month(5)
,_day(10)
{
_n = 1;//const类型成员必须在定义的时候就初始化,定义了之后不能改变
}
void Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
const int _n;
};
int main()
{
int i = 0;
Date d1(i);
d1.Print();
return 0;
}
在函数体中对const类型对象赋值会报错,所以const类型成员只能由初始化列表进行初始化。
定义了就要初始化的还有谁呢?那就是引用类型对象。
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Date
{
public:
Date(int& x, int year = 1, int month = 1, int day = 1)
:_year(2024)
, _month(5)
, _day(10)
, _n(5)
,xx(x) //c++中要求对引用类型成员定义的时候就初始化,是强制的,所以引用类型必须在初始化列表中初始化
{
//_n = 1;//const类型成员必须在定义的时候就初始化,定义了之后不能改变
}
void Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
const int _n;
int& xx;
};
int main()
{
int i = 0;
Date d1(i);
d1.Print();
return 0;
}
没有默认构造的自定义类型也必须在初始化列表中进行初始化。
如果自定义类型无默认构造也没有在初始化列表中进行初始化,编译就不能通过。
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Time
{
public:
Time(int hour,int minute,int second)
{
cout << "time()" << endl;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
Date(int& x, int year = 1, int month = 1, int day = 1)
:_year(2024)
, _month(5)
, _day(10)
, _n(5)
, xx(x) //c++中要求对引用类型成员定义的时候就初始化,是强制的,所以引用类型必须在初始化列表中初始化
{
//_n = 1;//const类型成员必须在定义的时候就初始化,定义了之后不能改变
}
void Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
const int _n;
int& xx;
Time _t;
};
int main()
{
int i = 0;
Date d1(i);
d1.Print();
return 0;
}
c++11支持在声明成员变量时给缺省值,这个缺失值是给没有在初始化列表中显示初始化的成员使用的 。
尽量使用初始化列表对成员变量进行初始化,那些你不在初始化列表初始化的成员也会走初始化列表,如果这个成员在声明位置给了缺省值,初始化列表会用这个缺省值初始化。如果你没有给缺省值,对于没有显示在初始化列表初始化的内置类型成员是否初始化取决于编译器,C++并没有规定。对于没有显示在初始化列表初始化的自定义类型成员会调用这个成员类型的默认构造函数,如果没有默认构造会编译错误。
总结:每个成员都要走初始化列表
1.在初始化列表初始化的成员
2.没有在初始化列表的成员
a.声明的地方有缺省值用缺省值
b.没有缺省值
x.内置类型。不确定,看编译器,大概率是随机值
y.自定义类型,调用默认构造,没有默认构造就编译报错
3.引用,const,没有默认构造的自定义类型 必须初始化列表初始化
初始化列表中按照成员变量在类中声明顺序进行初始化,跟成员在初始化列表出现的的先后顺序无关。建议声明顺序和初始化列表顺序保持⼀致。
观察下列程序并列出执行结果:
_a2比_a1先声明,所以_a2先初始化,A的构造函数中_a2用_a1的值初始化,这时候_a1没初始化,是随机值,所以用_a1的值初始化的_a2是随机值,之后用a的值初始化_a1,_a1为1。
类型转化
#include<iostream>
using namespace std;
class A
{
public:
// 构造函数explicit就不再支持隐式类型转换
// explicit A(int a1)
A(int a1)
:_a1(a1)
{}
//explicit A(int a1, int a2)
A(int a1, int a2)
:_a1(a1)
, _a2(a2)
{}
void Print()
{
cout << _a1 << " " << _a2 << endl;
}
private:
int _a1 = 1;
int _a2 = 2;
};
int main()
{
// 1构造⼀个A的临时对象,再用这个临时对象拷⻉构造aa3
// 编译器遇到连续构造+拷⻉构造->优化为直接构造
A aa1 = 1;
aa1.Print();
const A& aa2 = 1;
// C++11之后才支持多参数转化
A aa3 = { 2,2 };
return 0;
}
static成员
#define _CRT_SECURE_NO_WARNINGS 1
// 实现⼀个类,计算程序中创建出了多少个类对象?
#include<iostream>
using namespace std;
class A
{
public:
A()
{
++_scount;
}
A(const A& t)
{
++_scount;
}
~A()
{
--_scount;
}
static int GetACount()
{
return _scount;
}
private:
// 类里面声明
static int _scount;
};
// 类外面初始化
int A::_scount = 0;
int main()
{
cout << A::GetACount() << endl;
A a1, a2;
A a3(a1);
cout << A::GetACount() << endl;
cout << a1.GetACount() << endl;
// 编译报错:error C2248: “A::_scount”: ⽆法访问 private 成员(在“A”类中声明)
//cout << A::_scount << endl;
return 0;
}
通过类名::和对象 . 都可以找到GeyACount()函数
下面看一个题:
局部的全局变量在第一次运行到这个位置才会初始化,main之外的全局变量在main之前初始化。所以第一个选E;
成员变量后定义的先析构,所以先调用B的析构函数才调用A的析构函数;局部成员变量析构完之后才会析构全局变量,d在局部域,先调用D的析构函数。所以第二个选B。
友元
#include <iostream>
using namespace std;
class id;
//先声明id这个类,这样做是为了允许在 ID 类中声明 id 作为友元函数的参数类型,尽管 id 的完整定义在 //ID 的定义之后才出现。前向声明对于处理类之间的相互依赖很有用。
class ID
{
public:
friend void Func(const ID& a, const id& b);
ID(int I = 1, int W = 1, int H = 1)
:
_idnumber(I)
, _weight(W)
, _height(H)
{
}
void Print()
{
cout << _idnumber << _weight << _height << endl;
}
private:
int _idnumber;
int _weight;
int _height;
};
class id
{
public:
friend void Func(const ID& a, const id& b);
id(int I = 1, int W = 1, int H = 1)
:
_idnumber(I)
, _weight(W)
, _height(H)
{
}
void Print()
{
cout << _idnumber << _weight << _height << endl;
}
private:
int _idnumber;
int _weight;
int _height;
};
void Func(const ID &a,const id &b)
{
cout << a._height << endl;
cout << b._height << endl;
}
int main()
{
ID a1;
id a2;
Func(a1,a2);
return 0;
}
在定义ID类之前先声明id,这样做是为了允许在 ID
类中声明 id
作为友元函数的参数类型,尽管 id
的完整定义在 ID
的定义之后才出现。前向声明对于处理类之间的相互依赖很有用。
把一个类定义为另一个类的友元类之后可以单向访问另一个对象的私有成员。
#include <iostream>
using namespace std;
class id;
class ID
{
public:
friend class id;
friend void Func(const ID& a, const id& b);
ID(int I = 1, int W = 1, int H = 1)
:
_idnumber(I)
, _weight(W)
, _height(H)
{
}
void Print()
{
cout << _idnumber << _weight << _height << endl;
}
private:
int _idnumber;
int _weight;
int _height;
};
class id
{
public:
friend void Func(const ID& a, const id& b);
id(int I = 1, int W = 1, int H = 1)
:
_idnumber(I)
, _weight(W)
, _height(H)
{
}
void Print()
{
cout << _idnumber << _weight << _height << endl;
}
void Func2(const ID& ss)
{
cout << ss._idnumber << endl;//id为ID的友元类可以直接访问私有成员
}
private:
int _idnumber;
int _weight;
int _height;
};
void Func(const ID& a, const id& b)
{
cout << a._height << endl;
cout << b._height << endl;
}
int main()
{
ID a1;
id a2;
a2.Func2(a1);
return 0;
}
内部类
#include <iostream>
using namespace std;
class ID
{
public:
ID(int I = 1, int W = 1, int H = 1)
:
_idnumber(I)
, _weight(W)
, _height(H)
{
}
void Print()
{
cout << _idnumber << _weight << _height << endl;
}
class id
{
public:
id(int I = 1, int W = 1, int H = 1)
:
_idnumber(I)
, _weight(W)
, _height(H)
{
}
void Print()
{
cout << _idnumber << _weight << _height << endl;
}
void Func(ID& id_obj)
{
cout << id_obj._weight << endl;
}
private:
int _idnumber;
int _weight = 5;
int _height;
};
private:
int _idnumber;
int _weight;
int _height;
};
int main()
{
ID a1;
ID::id a2;
a2.Func(a1); // 通过对象 a1 访问 ID 类的私有成员
return 0;
}
将B定义在A的private里;B成为A的专属内部类其他地方无法使用
#include <iostream>
using namespace std;
class ID
{
public:
ID(int I = 1, int W = 1, int H = 1)
:
_idnumber(I)
, _weight(W)
, _height(H)
{
}
void Print()
{
cout << _idnumber << _weight << _height << endl;
}
private:
int _idnumber;
int _weight;
int _height;
class id
{
public:
id(int I = 1, int W = 1, int H = 1)
:
_idnumber(I)
, _weight(W)
, _height(H)
{
}
void Print()
{
cout << _idnumber << _weight << _height << endl;
}
void Func(ID& id_obj)
{
cout << id_obj._weight << endl;
}
private:
int _idnumber;
int _weight = 5;
int _height;
};
};
int main()
{
ID a1;
ID::id a2; //B放在了private里,成为A的专属内部类,其他地方无法访问
a2.Func(a1); // 通过对象 a1 访问 ID 类的私有成员
return 0;
}
匿名对象
#include <iostream>
using namespace std;
class Whatever
{
public:
int Func()
{
//......
}
void Print()
{
cout << _a << "/" << _b << endl;
}
private:
int _a = 2;
int _b = 1;
};
int main()
{
Whatever a;//有名对象
cout << a.Func() << endl;//用有名对象调用
//用匿名对象调用
cout << Whatever().Func() << endl;
}