简体   繁体   English

创建一个指向以结构为参数的成员函数的指针

[英]Create a pointer that points to a member function that takes a struct as an argument

I have main.cpp file where I defined a struct:我有 main.cpp 文件,我在其中定义了一个结构:

struct values {
        string name1;
        string name2;
        int key ;
        
    } a;

A class named Encrypt defined in a header file Encrypt.h and a file Encrypt.cpp where I define my functions... Im trying to create a pointer that points to a function that has a struct type as parameter here's how I did it: in my header file Encrypt.h在头文件 Encrypt.h 和一个文件 Encrypt.cpp 中定义了一个名为Encrypt的类,我在其中定义了我的函数...我试图创建一个指针,该指针指向一个具有结构类型作为参数的函数,我是这样做的:在我的头文件Encrypt.h


#ifndef Encrypt_h
#define Encrypt_h
#include<iostream>
using namespace std;
class Encrypt {
public:
    void print(void *);
};
#endif /* Encrypt_h */

in my Encrypt.cpp :在我的Encrypt.cpp中:

void Encrypt::print(void * args)
{
    struct values *a = args;
     string name= a.name1;

    cout<<"I'm"<<name<<endl;

};

here's how I tried to create the pointer in main.cpp这是我尝试在main.cpp中创建指针的方法

   void (Encrypt::* pointer_to_print) (void * args) = &Encrypt::print;

THE ERROR I GET :我得到的错误:

" Cannot initialize a variable of type 'struct values *' with an lvalue of type 'void *' " in Encrypt.cpp “不能用'void *'类型的左值初始化'struct values *'类型的变量”在Encrypt.cpp

the line :该行:

 struct values *a = args; 

REMARQUE评论

Im doing this because I want to pass a function with more than 2 parameters to the function : pthread_create() so my function print is just I simplified example.我这样做是因为我想将一个具有超过 2 个参数的函数传递给函数: pthread_create()所以我的函数 print 只是我简化的示例。

The problem is that args is of type void* and a is of type values* so you get the mentioned error.问题是argsvoid*类型,而avalues*类型,所以你会得到上面提到的错误。

To solve this uou can use static_cast to cast args to values* if you're sure that the cast is allowed:要解决此问题,如果您确定允许强制转换,您可以使用static_castargs转换为values*

values *a = static_cast<values*>(args);

Additionally change a.name1 to a->name1 .另外将a.name1更改为a->name1

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为成员函数定义代理,该成员函数将成员函数指针作为模板参数 - Defining a proxy for a member function that takes a member function pointer as a template argument 有什么方法可以创建一个 function 以成员 function 或成员作为参数? - Is there any way to create a function that takes as argument a member function or a member? 创建一个将函数指针作为参数的函数指针 - Create a function pointer which takes a function pointer as an argument 一个函数指针,该函数指针指向一个以模板类对象作为模板参数的函数。 可能? - A function pointer that points to a function that takes an object of a template class with said function pointer as template argument. Possible? C ++成员函数模板将成员函数指针作为模板参数 - C++ member function templates takes member function pointer as the template argument 将指向结构成员的指针传递给函数 - Passing pointer to a member of a struct to a function 将结构成员函数指针分配给类函数 - Assign struct member function pointer to class function C ++:指向结构内成员函数的指针 - C++: Pointer to Member Function within Struct 如何将结构成员指针传递给 function? - How to pass a struct member pointer to a function? 无法调用结构内的成员函数指针 - Unable to call member function pointer that is inside a struct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM