指向全局函数的C ++类成员指针

编程入门 行业动态 更新时间:2024-10-26 06:33:50
本文介绍了指向全局函数的C ++类成员指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想拥有一个类,该类具有指向函数的指针

I want to have a class which has as a member a pointer to a function

这是函数指针:

typedef double (*Function)(double);

这是一个适合函数指针定义的函数:

here is a function that fits the function pointer definition:

double f1(double x) { return 0; }

这是类别的定义:

class IntegrFunction { public: Function* function; };

在主函数中的某处我想做这样的事情:

and somewhere in the main function i want to do something like this:

IntegrFunction func1; func1.function = f1;

但是,此代码不起作用.

But, this code does not work.

是否可以向类成员分配指向如上所述的全局函数的函数指针?还是我必须更改函数指针定义中的某些内容?

Is it possible to assign to a class member a function pointer to a global function, declared as above? Or do I have to change something in the function pointer definition?

谢谢

推荐答案

替换此:

class IntegrFunction { public: Function* function; };

与此:

class IntegrFunction { public: Function function; };

您的typedef已经创建了一个函数指针.声明Function* function会创建一个指向函数的指针.

Your typedef already creates a pointer-to-function. Declaring Function* function creates a pointer-to-pointer-to-function.

更多推荐

指向全局函数的C ++类成员指针

本文发布于:2023-07-28 02:44:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   全局   函数   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!