typedef调用函数指针

typedef调用函数指针

一、typedef调用类外函数指针(类的静态函数指针也一样)

什么是函数指针?

函数名即表示函数指针,例如 void function(int a), 调用该函数时只需要键入 function(10), fun即表示函数指针——该函数代码所在的地址

typedef定义函数类型

typedef int function(int a);

function就表示一个函数类型,可以用function来声明和定义变量。

typedef定义函数指针类型

typedef int (*function)(int a);

function表示函数指针类型,指向函数代码所在的地址。

例子

#include <iostream>
#include <string>
using namespace std;

bool TestA(string a)
{
       cout << "The function TestA is used by " << a << endl;
       return true;
}

typedef bool(TestFunc)(string);
typedef bool(*TestFuncPointer)(string);

int main()
{
       /*Test the alias of the function*/
       TestFunc *test_func = TestA;
       test_func("the alias of the function");

       /*Test the alias of the function pointer*/
       TestFuncPointer test_func_pointer= TestA;
       test_func_pointer("the alias of the function pointer");
       system("pause");
       return 0;
}

参考

CSDN:typedef 函数类型详解


二、typedef调用类的非静态成员函数指针

类成员函数指针与普通函数指针的不同

因为非静态的成员函数必须被绑定到一个类的对象或者指针上,才能得到被调用。
所有类的对象都有自己的成员变量的拷贝,但是共用成员函数,为了区分哪一个对象调用了成员函数,就必须使用this指针,this指针是隐式的添加到函数参数列表里去的。

指针类型所需参数
指向函数的指针返回值类型、参数值类型及个数
指向类成员函数的指针返回值类型、所属的类的类型、参数值类型及个数

定义和使用指向类的成员函数的指针

/*定义别名*/
typedef 返回值 (类名::*指针类型名)(参数列表);

/*赋值*/
指针类型名 指针名=&类名::成员函数名;

/*调用*/
(类对象.*指针名)(参数列表); //调用对象是类对象
(类指针->*指针名)(参数列表); //调用对象是类指针

注意: 类成员函数名不能直接作为指针,需要用 & 取函数地址;因为不加&,编译器会认为是在这里调用成员函数,会要求给出参数列表,否则会报错;加了&,才认为是要获取函数指针。这是C++专门做了区别对待。

例子

类外的typedef及调用函数指针
#include <iostream>
#include <string>
using namespace std;

class TestClass
{
private:
       typedef bool (TestClass::*InClass_FunctionAlias)(string);
public:
       bool TestA(string a)
       {
               cout << "The member function TestA is used from " << a << endl;
               return true;
       }
};

typedef bool (TestClass::*FunctionAlias)(string);

int main()
{
       FunctionAlias test_function = &TestClass::TestA;
       
       TestClass *test_class_pointer = new TestClass;
       (test_class_pointer->*test_function)("a class pointer");
       //test_class_pointer->*test_function("a class pointer");//错误写法
       delete test_class_pointer;
       
       TestClass test_class;
       (test_class.*test_function)("a class object");
       //test_class.*test_function("the class object");//错误写法
       system("pause");
       return 0;
}

注意: 上述的错误写法,是因为没有加 () 造成的,因为是 (test_class.*test_function)这个整体指向的函数 的参数列表是(“a class object”) ,而括号的优先级高于成员操作符指针的优先级。

类内的typedef及调用函数指针
#include <iostream>
#include <string>
using namespace std;

class TestClass
{
private:
       typedef void (TestClass::*FunctionAlias)(string);

public:
       void TestB(string a)
       {
               cout << "The member function TestB is used from " << a << endl;
       }

       void TestInClassTypedef()
       {
               FunctionAlias test_function = &TestClass::TestB;
               //此处类名依然不可缺少!
               (this->*test_function)("a class pointer defined in the class ‘TestClass’ ");
               //使用this指针指向调用该函数TestInClassTypede的对象
       }
};

int main()
{
       TestClass test_class;
       test_class.TestInClassTypedef();

       system("pause");
       return 0;
}

参考

CSDN博客:C++类成员函数指针使用介绍

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值