C++新特性(3)

1.返回值优化技术

返回值优化技术本质上就是移动语义的应用,在代码运行中为了维护临时对象,对产生大量的创建和销毁等操作,严重影响运行性能,使用移动语义可以直接将右值过渡给目标对象,类似于剪切而不是复制,不用反复地调用构造和析构函数。

class String {
public:
	String() = default;
	String(const char* string)
	{
		std::cout << "调用构造函数!" << std::endl;
		m_Size = strlen(string);
		m_Data = new char[m_Size];
		memcpy(m_Data, string, m_Size);
	}
	
	String(const String& other)
	{
		std::cout << "调用拷贝函数!" << std::endl;
		m_Size = other.m_Size;
		m_Data = new char[m_Size];
		memcpy(m_Data, other.m_Data, m_Size);
	}

	~String()
	{
		std::cout << "调用析构函数!" << std::endl;
		delete m_Data;
	}

private:
	char* m_Data;
	int m_Size;
};


String func()
{
	String s("返回");
	return s;
}

int main()
{
	String result = func();
	return 0;
}

在这里插入图片描述此时func()函数是将对象s利用拷贝构造函数赋值给一个临时变量作为返回值,但是这个临时返回值并再调用拷贝构造函数赋值给result,而是直接将内容过渡给了result,两次调用析构函数实际上第一次是析构s,第二次是析构临时变量(result)

进一步优化可以使用move函数

String&& func()
{
	String s("返回");
	return std::move(s);
}

int main()
{
	String&& s = func();
	
	return 0;
}

在这里插入图片描述

2.闭包

闭包:闭包是带有上下文的函数,闭包的状态捆绑,必须发生在运行时。

闭包实现的方式:

  1. 仿函数:重载 operator()
  2. std::bind绑定器
  3. lambda表达式

1.仿函数:重载 operator()实现闭包
仿函数:仿函数就是使用函数对象代替函数指针

class Myfunc
{
    public:
    Myfunc(int x,int y):x(x),y(y){}
    int operator()(int z)
    {
       return (x+y)*z;
    }
    private:
    int x;
    int y;
};

int main()
{
    Myfunc mul(2, 5);
    cout << mul(3) << endl;
    return 0;
}

在这里插入图片描述2.bind绑定器

  1. std::function
    function<函数返回类型(参数类型)>
    头文件:#include < functional >
void func()
{
    cout << "_func_" << endl;
}
class A
{
public:
    static int testA(int a)
    {
        cout << "testA:" << a << endl;
        return a;
    }
};
class B
{
public:
    int operator()(char b)
    {
        cout << "B:" << b - 'a' << endl;
        return b - 'a';
    }
};

int main()
{
    function<void()> f1 = func;
    f1();
    function<int(int)> f2 = A::testA;
    f2(10);

    B b;
    function<int(char)> f3 = b;
    f3('b');
}

在这里插入图片描述
2.bind绑定器

void func(int x, int y)
{
	cout << x << " " << y << endl;
}
int main()
{
	bind(func, 11, 22)();
	bind(func, std::placeholders::_1, std::placeholders::_2)(11, 22);
	using namespace std::placeholders;
	bind(func, 11, _1)(22);
	bind(func, _2, 22)(22,11);
	return 0;
}

_1表示被传进来的第一个参数所替换,_2表示被传进来的第二个参数所替换
在这里插入图片描述
3.bind和function结合使用

using namespace std;
using namespace std::placeholders;

class Test
{
public:
    void func(int x, int y)
    {
        cout << x << " " << y << endl;
    }
    int a;
};

int main()
{
    Test obj;
    //绑定成员函数
    function<void(int, int)> f1 = bind(&Test::func, &obj, _1, _2);
    f1(11, 22);
    function<int &()> f2 = bind(&Test::a, &obj);
    f2() = 111;
    cout << obj.a << endl;
}

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值