可调用对象

1, 可调用对象->普通函数

#include <iostream>
using namespace std;

// 可调用对象->普通函数

using Fun = void(int, const string&);   // 普通函数类型的别名
Fun show;                               // 声明函数类型

int main()
{
    show(1, "我是一只傻鸟");      // 直接调用普通函数

    // c风格调用
    void(*fp1)(int, const string&) = show;  // 声明函数指针,指向普通函数。
    void(&fr1)(int, const string&) = show;  // 声明函数引用,引用普通函数.
    fp1(2, "我是一只傻鸟");                   // 用函数指针调用普通函数。
    fr1(3, "我是一只傻鸟");                   // 用函数引用调用普通函数。

    Fun *fp2 = show;                        // 声明函数指针,指向普通函数
    Fun &fr2 = show;                        // 声明函数引用,引用普通函数
    fp2(4, "我是一只傻鸟");                   // 用函数指针调用普通函数
    fr2(5, "我是一只傻鸟");                   // 用函数引用调用普通函数

    return 0;
}

// 定义普通函数
void show(int bh, const string& message) {
    cout << "亲爱的 " << bh << "号," << message << endl;
}

2, 类的静态成员函数

#include <iostream>
using namespace std;

// 可调用对象->普通函数

using Fun = void(int, const string&);   // 普通函数类型的别名
struct AA   // 类中有静态成员函数。
{
    static void show(int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    }
};

int main()
{
    AA::show(1, "我是一只傻傻鸟。");    // 直接调用静态成员函数。

    void(*fp1)(int, const string&) = AA::show;  // 用函数指针指向静态成员函数。
    void(&fr1)(int, const string&) = AA::show;  // 引用静态成员函数。
    fp1(2, "我是一只傻傻鸟。");                 // 用函数指针调用静态成员函数。
    fr1(3, "我是一只傻傻鸟。");                 // 用函数引用调用静态成员函数。

    Fun* fp2 = AA::show;                        // 用函数指针指向静态成员函数。
    Fun& fr2 = AA::show;                        // 引用静态成员函数。
    fp2(4, "我是一只傻傻鸟。");                 // 用函数指针调用静态成员函数。
    fr2(5, "我是一只傻傻鸟。");                 // 用函数引用调用静态成员函数。

    return 0;
}

3, 仿函数

#include <iostream>
using namespace std;
// 仿函数的类型就是类的类型
struct BB   // 仿函数。
{
    void operator()(int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    }
};

int main()
{
    BB bb;
    bb(11, "我是一只傻傻鸟。");     // 用对象调用仿函数。
    BB()(12, "我是一只傻傻鸟。");       // 用匿名对象调用仿函数。

    BB& br = bb;                // 引用函数
    br(13, "我是一只傻傻鸟。");     // 用对象的引用调用仿函数。
}

4, lambda函数

#include <iostream>
using namespace std;

int main()
{
    // 创建lambda对象。
    auto lb = [](int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    };

    auto& lr = lb;  // 引用lambda对象。

    lb(111, "我是一只傻傻鸟。");        // 用lambda对象调用仿函数。
    lr(222, "我是一只傻傻鸟。");        // 用lambda对象的引用调用仿函数。
}

5, 类的非静态成员函数

#include <iostream>
using namespace std;
/*
 * 类的非静态成员函数有地址,但是,只能通过类的对象才能调用它,
 * 所以,C++对它做了特别处理。
 * 类的非静态成员函数只有指针类型,没有引用类型,不能引用
 */

struct CC   // 类中有普通成员函数。
{
    void show(int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    }
};

int main()
{
    CC cc;
    cc.show(14, "我是一只傻傻鸟。");

    void (CC::* fp11)(int, const string&) = &CC::show;  // 定义类的成员函数的指针。
    (cc.*fp11)(15, "我是一只傻傻鸟。");                 // 用类的成员函数的指针调用成员函数。

    using pFun = void (CC::*)(int, const string&);      // 类成员函数的指针类型。
    pFun fp12 = &CC::show;                              // 让类成员函数的指针指向类的成员函数的地址。
    (cc.*fp12)(16, "我是一只傻傻鸟。");             // 用类成员函数的指针调用类的成员函数。
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容