栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

typedef调用函数指针

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

typedef调用函数指针

typedef调用函数指针 文章目录
  • typedef调用函数指针
    • @[toc]
    • 一、typedef调用类外函数指针(类的静态函数指针也一样)
      • 什么是函数指针?
      • typedef定义函数类型
      • typedef定义函数指针类型
      • 例子
      • 参考
    • 二、typedef调用类的非静态成员函数指针
      • 类成员函数指针与普通函数指针的不同
      • 定义和使用指向类的成员函数的指针
      • 例子
        • 类外的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 
#include 
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()
{
       
       TestFunc *test_func = TestA;
       test_func("the alias of the function");

       
       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 
#include 
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 
#include 
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++类成员函数指针使用介绍

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004228.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号