指向不是类成员的函数的指针(是类成员)

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

double getSalary(int v,double s){return v*s;} double getSalary(double s){return s/2;} double getSalary(){return 1000;} class person{ //the class is not inherited, so I can''t use virtual public: char name[20]; int age; double salary; person(char* num="Anonim",int a=0,double s=0):age(a),salary(s) { strcpy(name,num); } //...methods... double (*person::getSalary)() //I have also tried without personand many others... }; void main(){ person p("John",25,3000); cout<<"Salary is "<<p.getSalary /*not sure that this is the right way to call it, but i don''t think this is the main mistake, also please correct me here*/ }

请告诉我如何在类中声明一个指向getSalary()的指针,在此示例中,我需要函数的第3个版本.我认为编译器会基于函数自动知道要调用的3个之一'' s签名.我没有编译错误,但在运行时会刹车.我尝试调试它,它表明指针为空.

Please tell me how to declare inside the class a pointer to getSalary(),in this example I want the 3rd version of the function.I thought that the compiler automatically knows wich one of the 3 I want to call, baseing on function''s signature.I have no compileing error but it brakes on run-time.I have tried to debug it, it seams that the pointer is empty.

推荐答案

未初始化函数指针person::getSalary.取消引用此指针会导致崩溃. 在调用p.getSalary();之前使用p.getSalary= getSalary;,希望编译器知道选择哪种重载. You do not initialize the function pointer person::getSalary. Dereferencing this pointer leads to a crash. Use p.getSalary= getSalary; before invoking p.getSalary(); hoping that the compiler will know what overload to pick.

您的示例将始终仅应调用 Hi, Your example will always should call getSalary()

. 因为您的函数指针是

only. Because your function pointer is of type

(*person::getSalary)()

类型,其中没有参数. 我没有用真实的代码对其进行测试,但这在逻辑上是隐含的.

where there is no parameters. I have not tested it with the real code , but this is what logically implies.

该示例有几处错误,但我会坚持提出的问题. 声明"double (*person::getSalary)();"不应已编译. 指向成员函数的指针的声明将是 double (person::*getSalary)();. 由于您的代码示例暗示您要指向全局函数,因此正确的声明应为 double (*getSalary)();. 以下内容将为您提供总体思路. There are several things wrong with the example, but I will stick with the question asked. The declaration "double (*person::getSalary)();" should not have compiled. The declaration of a pointer to a member function would be double (person::*getSalary)();. Since your code example implies you want to point to a global function the correct declaration would be double (*getSalary)();. The following should give you the general idea. #include <cstring> double getSalary(int v, double s) { return v * s; } double getSalary(double s) { return s/2; } double getSalary() { return 1000; } class person { public: char name[20]; int age; double salary; person(char* num="Anonim",int a=0, double s=0) : age(a), salary(s) { strcpy(name,num); // dangerous: length of num is unknown getSalary = ::getSalary; getIt = ::getSalary; } // pointer to global function with signature of // double (*)() double (*getSalary)(); // same thing with a different varaible name for the pointer double (*getIt)(); }; void test() { person p("John",25,3000); std::cout << "Salary is " << p.getSalary() << std::endl; std::cout << "Salary is " << p.getIt() << std::endl; }

更多推荐

指向不是类成员的函数的指针(是类成员)

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

发布评论

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

>www.elefans.com

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