自C++11引入了std:bind和std::function这是两个非常重要的东西.
我们首先来看std::bind
#include <iostream>
#include <functional>
class MyMultiply{
private:
int a;
int b;
public:
MyMultiply()=default;
MyMultiply(const int& lh, const int& rh):a(lh),b(rh){}
~MyMultiply()=default;
void operator()(const int& lh, const int& rh)noexcept
{
std::cout<< a * b * lh * rh <<std::endl;
}
};
void divide(const int& lh, const int& rh)
{
std::cout<< lh - rh <<std::endl;
}
struct MyPlus{
public:
int a;
int b;
MyPlus(const int& lh, const int& rh):a(lh),b(rh){}
MyPlus()=default;
~MyPlus()=default;
void print(const int& lh, const int& rh)
{
std::cout<< a*b*lh*rh <<std::endl;
}
};
int main()
{
//case 1:
auto funOne = std::bind(divide, std::placeholders::_1, std::placeholders::_2);
funOne(50, 40); //50 - 40.
//case 2:
auto funcTwo = std::bind(divide, std::placeholders::_2, std::placeholders::_1);
funcTwo(40, 30); //30 - 40.
//case 3:
auto funcThree = std::bind(divide, std::placeholders::_1, 30);
funcThree(50); //50 - 30.
//case 4:
auto funcFour = std::bind(divide, 30, std::placeholders::_1);
funcFour(20); //30 - 20.
//case 5:
auto funcFive = std::bind(MyMultiply(20 , 30), std::placeholders::_1, std::placeholders::_2);
funcFive(30, 40);
MyPlus plus(20, 30);
//case 6: bind to member data.
auto bindMemData = std::bind(&MyPlus::a, plus);
std::cout<< bindMemData() <<std::endl; //std::cout<< plus.a <<std::endl;
//case 7:
auto bindMData = std::bind(&MyPlus::b, std::placeholders::_1);
bindMData(plus); //std::cout<< plus.b <<std::endl;
//case 8:
auto bindMemFunOne = std::bind(&MyPlus::print, plus, std::placeholders::_1, std::placeholders::_2);
bindMemFunOne(20, 30); //plus.print(20, 30);
//case 9:
auto bindMemFunTwo = std::bind(&MyPlus::print, plus, 20, 20);
bindMemFunTwo(); //plus.print(20, 20);
//case 10:
auto lambda = std::bind([](const int& number){ std::cout<< number <<std::endl; }, std::placeholders::_1);
lambda(20);
return 0;
}
std::function:
#include <iostream>
#include <functional>
void half(const int& lh, const int& rh)
{
std::cout << (lh + rh) / 2 << std::endl;
}
class Half {
private:
int a;
int b;
public:
Half() = default;
Half(const int& lh, const int& rh) :a(lh), b(rh) {}
~Half() = default;
Half(const Half& other) :a(other.a), b(other.b) {}
Half(Half&& other) :a(std::move(other.a)), b(std::move(other.b)) {}
Half& operator=(const Half& other)
{
this->a = other.a;
this->b = other.b;
return *this;
}
Half& operator=(Half&& other)
{
this->a = std::move(other.a);
this->b = std::move(other.b);
return *this;
}
void operator()(const int& lh, const int& rh)
{
std::cout << lh*rh << std::endl;
}
};
struct Plus {
public:
int a;
int b;
Plus() = default;
~Plus() = default;
Plus(const int& lh, const int& rh) :a(lh), b(rh) {}
Plus(const Plus& other) :a(other.a), b(other.b) {}
Plus(Plus&& other):a(std::move(other.a)), b(std::move(other.b)) {}
Plus& operator=(const Plus& other)
{
this->a = other.a;
this->b = other.b;
return *this;
}
Plus& operator=(Plus&& other)
{
this->a = std::move(other.a);
this->b = std::move(other.b);
return *this;
}
int print()
{
return (a + b);
}
int printf()
{
std::cout << "test" << std::endl;
this->a = 520;
this->b = 520;
return (this->a + this->b);
}
void printNumber(const int& num)
{
std::cout << num << std::endl;
std::cout << this->a << " " << this->b << std::endl;
}
};
class Wrap{
public:
Wrap(const int& value) :number(value) {}
void printNumber(const int& n)const
{
std::cout << n << " " << this->number << std::endl;
}
int number;
};
int main()
{
Plus plus(20, 30);
/*
//case 1:
std::function<void(const int&, const int&)> funcOne = half;
funcOne(20, 30);
//case 2:
std::function<void(const int&, const int&)> funcTwo = ½
funcTwo(40, 50);
//case 3:
std::function<void(const int&, const int&)> funcThree = Half(50, 60);
funcThree(50, 60);
//case 4:
std::function<int(const Plus&)> funcFour = &Plus::a;
funcFour(plus);
*/
//case 5:
std::function<void(Plus&)> funcFive = &Plus::printf; //OK.
funcFive(plus);
//plus.printNumber(20); //输出 Plus::a : 520, Plus::b : 520;
//case 6:
std::function<int(Plus*)> funcSix = &Plus::print;
std::cout << funcSix(&plus) << std::endl;
//case 7:
std::function<void(Plus&, int)> funcSeven = &Plus::printNumber;
funcSeven(plus, 20);
//case 8:
Wrap wrap(40);
std::function<void(const Wrap&, const int&)> funcEight = &Wrap::printNumber;
funcEight(wrap, 20);
//case 9:
std::function<void(const Wrap*, const int&)> funcNinth = &Wrap::printNumber;
funcNinth(&wrap, 40);
return 0;
}