7. using
的使用
在C++中using
用于声明命名空间,在C++11中赋予using
新的功能。可以同typedef
一样定义别名。
- 定义别名
//typedef 定义别名
typedef 旧的类型名 新的类型名
typedef unsigned int uint_t;
//using 定义别名
using 新的类型 = 旧的类型
using uint_t = int;
通过上述语法格式可以看出两者没有太大区别,假设我们定义一个函数指针,using
的优势就能凸显出来了:
//使用typedef定义函数指针
typedef int(*func_ptr)(int,double);
//使用using定义函数指针
using func_ptr = int(*)(int,double);
使用using
定义函数指针别名的写法看起来就非常直观了
- 模板别名
使用typedef重定义类似很方便,但是它有一点限制,比如无法重定义一个模板,比如我们需要一个固定以int类型为key的map,它可以和很多类型的value值进行映射,如果使用typedef这样直接定义就非常麻烦。
typedef map<int,string>m1;
typedef map<int,int>m2;
typedef map<int,double>m3;
typedef map<int,float>m4;
但如果直接使用模板的话,语法不会通过。
template<typename T>
typedef map<int,T>type; //error,语法错位
如果想强行使用的话,需要一个外敷类进行包裹
template<typename T>
struct myMap{
typedef map<int,T> type;
};
//使用
myMap<string>::type m;
m.insert(make_pair(1,'zsj'));
上述例子能够发现,需求简单但实现起来并不容易,于是我们可以用C++11
对using
新增的一个特性,能够为一个模板定义别名:
template<typename T>
using myMap = map<int,T>;
//使用
myMap<string>m;
m.insert(make_pair(10,'zsj'));
8. 委托构造函数和继承构造函数
- 委托构造函数
委托构造函数允许使用同一个类中的一个构造函数调用其它的构造函数,从而简化相关变量的初始化。
class Test
{
public:
Test() {};
Test(int max)
{
this->m_max = max > 0 ? max : 100;
}
Test(int max, int min)
{
this->m_max = max > 0 ? max : 100; // 冗余代码
this->m_min = min > 0 && min < max ? min : 1;
}
Test(int max, int min, int mid)
{
this->m_max = max > 0 ? max : 100; // 冗余代码
this->m_min = min > 0 && min < max ? min : 1; // 冗余代码
this->m_middle = mid < max && mid > min ? mid : 50;
}
int m_min;
int m_max;
int m_middle;
};
在上面三个构造函数都有重复的代码,在C++11
加入了委托构造之后,我们就可以轻松地完成代码的优化了:
Test(int max, int min):Test(max)
{
this->m_min = min > 0 && min < max ? min : 1;
}
Test(int max, int min, int mid):Test(max, min)
{
this->m_middle = mid < max && mid > min ? mid : 50;
}
- 继承构造函数
C++11
中提供的继承构造函数可以让派生类直接使用基类的构造函数,而无需自己再写构造函数,尤其是在基类有很多构造函数的情况下,可以极大地简化派生类构造函数的编写。以下是没有继承构造函数之前的处理方式:
class Base
{
public:
Base(int i) :m_i(i) {}
Base(int i, double j) :m_i(i), m_j(j) {}
Base(int i, double j, string k) :m_i(i), m_j(j), m_k(k) {}
int m_i;
double m_j;
string m_k;
};
class Child : public Base
{
public:
Child(int i) :Base(i) {}
Child(int i, double j) :Base(i, j) {}
Child(int i, double j, string k) :Base(i, j, k) {}
};
通过测试代码可以看出,在子类初始化从基类继承的类成员,需要在子类中重新定义和基类一致的构造函数,这是非常繁琐的。使用继承构造这个新特性可以很完美的解决这个问题。
//语法
using 类命::构造函数名(类命和构造函数名相同);
于是可以将上述child
中的代码改为:
class Child : public Base
{
public:
using Base::Base;
};
另外如果在子类中隐藏了父类中的同名函数,也可以通过using
的方式在子类中使用基类中的这些父类函数
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
Base(int i) :m_i(i) {}
Base(int i, double j) :m_i(i), m_j(j) {}
Base(int i, double j, string k) :m_i(i), m_j(j), m_k(k) {}
void func(int i)
{
cout << "base class: i = " << i << endl;
}
void func(int i, string str)
{
cout << "base class: i = " << i << ", str = " << str << endl;
}
int m_i;
double m_j;
string m_k;
};
class Child : public Base
{
public:
using Base::Base;
using Base::func;
void func()
{
cout << "child class: i'am luffy!!!" << endl;
}
};
int main()
{
Child c(250);
c.func();
c.func(19);
c.func(19, "luffy");
return 0;
}
9. 列表初始化
-
统一的初始化
在
C++98/03
中,对应普通数组时可以使用列表初始化来初始化数据的。
//数组初始化
int array[] = {1,2,3,4,5,6,7};
double array1[3] = {1.1,2.1,3.1};
//对象初始化
struct Person{
int id;
double salary;
}man{1,3000};
在C++11
中,列表初始化变得更加灵活:
class test{
public:
test(int){}
private:
test(const test& t);
};
void testfunc(){
test t1(10);
test t2 = 20; //隐式类型转换
test t3 = {30}; //列表初始化
//使用new操作符创建新对象使用列表初始化进行对象初始化
int* p = new int{50};
double b = double{52.13};
int * array = new int[3]{1,2,3};
}
- 列表初始化细节
- 聚合体
在C++11
中,列表初始化的适用范围被大大增强了,但一些模糊的概念也随之而来,从前面的例子可知,列表初始化可以用于非自定义类型的初始化,但是对一个自定义类型,列表初始化可能有两种执行结果:
#include <iostream>
#include <string>
using namespace std;
struct T1
{
int x;
int y;
}a = { 123, 321 };
struct T2
{
int x;
int y;
T2(int, int) : x(10), y(20) {}
}b = { 123, 321 };
int main(void)
{
cout << "a.x: " << a.x << ", a.y: " << a.y << endl;
cout << "b.x: " << b.x << ", b.y: " << b.y << endl;
return 0;
}
程序执行结果:
a.x: 123, a.y: 321
b.x: 10, b.y: 20
在上边的程序中都是用列表初始化的方式对对象进行了初始化,但是得到结果却不同,对象b
并没有被初始化列表中的数据初始化,这是为什么呢?因为如果使用列表初始化对对象初始化时,还需要判断这个对象对应的类型是不是一个聚合体,如果是初始化列表中的数据就会拷贝到对象中。
聚合体的分类:
//1.普通数组
//2.满足以下条件的类(class、struct、union)可以被看成是一个聚合类型
2.1. 无用户自定义构造函数
2.2. 无私有或保护的非静态数据成员
2.3. 无基类
2.4. 无虚函数
2.5. 类中不能有使用{}和 = 直接初始化的非静态数据成员(从C++14开始支持)
当类中有静态成员时,静态成员不能使用列表初始化进行初始化,它的初始化遵循静态成员的初始化方式。
struct test{
public:
int x;
int y;
private:
static int y;
}t{10,20};
int test::y = 5;
- 列表初始化细节
- 非聚合体
对于聚合类型的类可以直接使用列表初始化进行对象的初始化,如果不满足聚合条件还想使用列表初始化其实也是可以的,需要在类内部自定义一个构造函数,在构造函数中使用初始化列表对类成员变量进行初始化。
struct T1
{
int x;
double y;
// 在构造函数中使用初始化列表初始化类成员
T1(int a, double b, int c) : x(a), y(b), z(c){}
virtual void print()
{
cout << "x: " << x << ", y: " << y << ", z: " << z << endl;
}
private:
int z;
};
10. 基于范围的for
循环
在C++98/03
中,不同的容器和数组遍历的方式不尽相同,写法不统一,也不够简洁,而C++11
基于范围的for
循环可以以简洁、统一的方式来遍历容器和数组,用起来也更方便了。
for
循环新语法
//C++98/03中普通的for循环语法
for(表达式 1; 表达式 2; 表达式 3)
{
// 循环体
}
//C++11 基于范围的for循环语法
for (declaration : expression)
{
// 循环体
}
在上面的语法格式中declaration
表示遍历声明,在遍历过程中,当前被遍历到的元素会被存储到声明的变量中。expression
是要遍历的对象,它可以是表达式、容器、数组、初始化列表等。
vector<int> v{ 1,2,3,4,5,6 };
// elem 为遍历到的元素的拷贝
for(auto elem: v)
{
cout << elem << " ";
}
// elem 为遍历到的元素的引用
for (auto &elem : v)
{
cout << elem << " ";
}
- 使用细节
- 关系型容器
使用基于范围的 for 循环有一些需要注意的细节,先来看一下对关系型容器 map 的遍历:
map<int, string> m{
{1, "lucy"},{2, "lily"},{3, "tom"}
};
// 基于范围的for循环方式
for (auto& it : m)
{
cout << "id: " << it.first << ", name: " << it.second << endl;
}
// 普通的for循环方式
for (auto it = m.begin(); it != m.end(); ++it)
{
cout << "id: " << it->first << ", name: " << it->second << endl;
}
使用普通的for
循环方式(基于迭代器)遍历关联性容器,auto
自动推导出的是一个迭代器类型,需要使用迭代器的方式取出元素中的键值对(和指针的操作方法相同)
使用基于范围的for
循环遍历关联性容器,auto
自动推导出的类型是容器中的 value_type
,相当于一个对组(std::pair)
对象。
使用细节
- 访问次数
#include <iostream>
#include <vector>
using namespace std;
vector<int> v{ 1,2,3,4,5,6 };
vector<int>& getRange()
{
cout << "get vector range..." << endl;
return v;
}
int main(void)
{
for (auto val : getRange())
{
cout << val << " ";
}
cout << endl;
return 0;
}
输出结果:
get vector range...
1 2 3 4 5 6
从上面的结果中可以看到,不论基于范围的for
循环迭代了多少次,函数 getRange ()
只在第一次迭代之前被调用,得到这个容器对象之后就不会再去重新获取这个对象了。
11. 可调用对象包装器、绑定器
C++
中可调用对象分为以下几类:
- 可调用对象
- 是一个函数指针
- 是一个具有
operator()
成员函数的类对象(仿函数) - 是一个可被转换为函数指针的类对象
- 是一个类成员函数指针或者类成员指针
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int testFunc(int a,double b){
cout<<a<<b<<endl;
return 0;
}
using funcPtr = void(*)(int,double);
struct Test{
// 2. ()操作符重载
void operator()(string msg){
cout<<"msg: "<<msg<<endl;
}
static void print1(int a,string b){
cout<<a<<b<<endl;
}
void print2(int a,string b){
cout<<a<<b<<endl;
}
// 3. 将类对象转换成函数指针
operator funcPtr(){
//return print2; //error 返回的是对象方法
return print1; // 返回的函数必须是static关键字修饰的,需要返回一个类方法,返回的是对象方法会报错
}
int _id;
};
void callabledObj(){
// 1.定义函数指针
int(*fPtr1)(int,double) = &testFunc;
// 3.对象转换为函数指针并调用
Test t;
t(19,"Unreal");
// 4.是一个类成员函数指针或者类成员指针
// 定义类成员函数指针指向类成员函数
void(Test::*fPtr2)(int,string) = &Test::print2;
// 类成员指针指向类成员变量
int Test::*obj_Ptr = &Test::_id;
// 通过类成员函数指针调用类成员函数
(t.*fptr2)(19,"unreal"); // 对fptr2解引用
// 通过类成员指针初始化类成员变量
t.*obj_Ptr = 1;
}
在上面的例子中满足条件的这些可调用对象对应的类型被统称为可调用类型。C++
中的可调用类型虽然具有比较统一的操作形式,但定义方式五花八门,这样在我们试图使用统一的方式保存,或者传递一个可调用对象时会十分繁琐。现在,C++11
通过提供std::function
和 std::bind
统一了可调用对象的各种操作。
- 可调用对象包装器
std::function
是可调用对象的包装器。它是一个类模板,可以容纳除了类成员(函数)指针之外的所有可调用对象。通过指定它的模板参数,它可以用统一的方式处理函数、函数对象、函数指针,并允许保存和延迟执行它们。
//基本用法
#include <functional>
std::function<返回值类型(参数类型列表)> diy_name = 可调用对象;
下列实例代码演示了基本使用方法:
#include <iostream>
#include <functional>
using namespace std;
int add(int a, int b)
{
cout << a << " + " << b << " = " << a + b << endl;
return a + b;
}
class T1
{
public:
static int sub(int a, int b)
{
cout << a << " - " << b << " = " << a - b << endl;
return a - b;
}
};
class T2
{
public:
int operator()(int a, int b)
{
cout << a << " * " << b << " = " << a * b << endl;
return a * b;
}
};
int main(void)
{
// 绑定一个普通函数
function<int(int, int)> f1 = add;
// 绑定以静态类成员函数
function<int(int, int)> f2 = T1::sub;
// 绑定一个仿函数
T2 t;
function<int(int, int)> f3 = t;
// 函数调用
f1(9, 3);
f2(9, 3);
f3(9, 3);
return 0;
}
通过测试代码可以得到结论:std::function
可以将可调用对象进行包装,得到一个统一的格式,包装完成得到的对象相当于一个函数指针,和函数指针的使用方式相同,通过包装器对象就可以完成对包装的函数的调用了。
作为回调函数使用:
class A{
public:
// 构造函数参数是一个包装器对象
A(const function<void()>& f):callback(f){};
void notify(){
callback();
}
private:
function<void()> callback;
};
class B{
public:
void operator()(){
cout<<"Call back function!"<<endl;
}
};
void callBackFunc(){
B b;
A a(b);
a.notify();
}
通过上面的例子可以看出,使用对象包装器 std::function
可以非常方便的将仿函数转换为一个函数指针,通过进行函数指针的传递,在其他函数的合适的位置就可以调用这个包装好的仿函数了。另外,使用std::function
作为函数的传入参数,可以将定义方式不相同的可调用对象进行统一的传递,这样大大增加了程序的灵活性。
- 绑定器
std::bind
用来将可调用对象与其参数一起进行绑定。绑定后的结果可以使用std::function
进行保存,并延迟调用到任何我们需要的时候。通俗来讲,它主要有两大作用:
--1.将可调用对象与其参数一起绑定成一个仿函数。
--2.将多元(参数个数为n,n>1)可调用对象转换为一元或者(n-1)元可调用对象,即只绑定部分参数。
绑定器函数使用语法格式如下:
// 绑定非类成员函数/变量
auto f = std::bind(可调用对象地址, 绑定的参数/占位符);
// 绑定类成员函/变量
auto f = std::bind(类函数/成员地址, 类实例对象地址, 绑定的参数/占位符);
下面来看一个关于绑定器的实际使用的例子:
#include <iostream>
#include <functional>
using namespace std;
void callFunc(int x, const function<void(int)>& f)
{
if (x % 2 == 0)
{
f(x);
}
}
void output(int x)
{
cout << x << " ";
}
void output_add(int x)
{
cout << x + 10 << " ";
}
int main(void)
{
// 使用绑定器绑定可调用对象和参数
auto f1 = bind(output, placeholders::_1);
for (int i = 0; i < 10; ++i)
{
callFunc(i, f1);
}
cout << endl;
auto f2 = bind(output_add, placeholders::_1);
for (int i = 0; i < 10; ++i)
{
callFunc(i, f2);
}
cout << endl;
return 0;
}
输出结果:
0 2 4 6 8
10 12 14 16 18
在上面的程序中,使用了 std::bind
绑定器,在函数外部通过绑定不同的函数,控制了最后执行的结果。std::bind
绑定器返回的是一个仿函数类型,得到的返回值可以直接赋值给一个std::function
,在使用的时候我们并不需要关心绑定器的返回值类型,使用auto进行自动类型推导就可以了。placeholders::_1
是一个占位符,代表这个位置将在函数调用时被传入的第一个参数所替代。同样还有其他的占位符placeholders::_2、placeholders::_3、placeholders::_4、placeholders::_5
等……
有了占位符的概念之后,使得std::bind
的使用变得非常灵活:
#include <iostream>
#include <functional>
using namespace std;
void output(int x, int y)
{
cout << x << " " << y << endl;
}
int main(void)
{
// 使用绑定器绑定可调用对象和参数, 并调用得到的仿函数
bind(output, 1, 2)();
bind(output, placeholders::_1, 2)(10);
bind(output, 2, placeholders::_1)(10);
// error, 调用时没有第二个参数
// bind(output, 2, placeholders::_2)(10);
// 调用时第一个参数10被吞掉了,没有被使用
bind(output, 2, placeholders::_2)(10, 20);
bind(output, placeholders::_1, placeholders::_2)(10, 20);
bind(output, placeholders::_2, placeholders::_1)(10, 20);
return 0;
}
示例代码执行的结果:
1 2 // bind(output, 1, 2)();
10 2 // bind(output, placeholders::_1, 2)(10);
2 10 // bind(output, 2, placeholders::_1)(10);
2 20 // bind(output, 2, placeholders::_2)(10, 20);
10 20 // bind(output, placeholders::_1, placeholders::_2)(10, 20);
20 10 // bind(output, placeholders::_2, placeholders::_1)(10, 20);
通过测试可以看到,std::bind
可以直接绑定函数的所有参数,也可以仅绑定部分参数。在绑定部分参数的时候,通过使用std::placeholders
来决定空位参数将会属于调用发生时的第几个参数。
可调用对象包装器 std::function
是不能实现对类成员函数指针或者类成员指针的包装的,但是通过绑定器 std::bind
的配合之后,就可以完美的解决这个问题了,再来看一个例子,然后再解释里边的细节:
#include <iostream>
#include <functional>
using namespace std;
class Test
{
public:
void output(int x, int y)
{
cout << "x: " << x << ", y: " << y << endl;
}
int m_number = 100;
};
int main(void)
{
Test t;
// 绑定类成员函数
function<void(int, int)> f1 =
bind(&Test::output, &t, placeholders::_1, placeholders::_2);
// 绑定类成员变量(公共)
function<int&(void)> f2 = bind(&Test::m_number, &t);
// 调用
f1(520, 1314);
f2() = 2333;
cout << "t.m_number: " << t.m_number << endl;
return 0;
}
示例代码输出的结果:
x: 520, y: 1314
t.m_number: 2333
在用绑定器绑定类成员函数或者成员变量的时候需要将它们所属的实例对象一并传递到绑定器函数内部。f1
的类型是function<void(int, int)>
,通过使用std::bind
将Test
的成员函数output
的地址和对象t
绑定,并转化为一个仿函数并存储到对象f1
中。
使用绑定器绑定的类成员变量m_number
得到的仿函数被存储到了类型为function<int&(void)>
的包装器对象f2
中,并且可以在需要的时候修改这个成员。其中int
是绑定的类成员的类型,并且允许修改绑定的变量,因此需要指定为变量的引用,由于没有参数因此参数列表指定为void
。
示例程序中是使用function
包装器保存了bind
返回的仿函数,如果不知道包装器的模板类型如何指定,可以直接使用 auto
进行类型的自动推导,这样使用起来会更容易一些。