c++ void* 作为函数的参数

发布于 2024-12-24 22:09:46 字数 604 浏览 5 评论 0原文

我在某个库中有这个函数:

class myConsole
{
    void addCommand( std::string command, void* fn );
    ...
}

在我的类中我有这个函数:

void myApp::TestFn( const std::vector<std::string> & args )
{
    // do something
}

在同一个类中我称之为:

void myApp::initApp( )
{
    myConsole::getSingleton( ).addCommand( "FirstTest", &myApp::TestFn );
}

但这给了我这个错误:

错误 c2664 无法将参数 2 从 'void(__thiscall myApp::*)(const std::vector<_Ty>&)' 到 'void *'

我该如何解决这个问题?

提前致谢!

I have this function in some library:

class myConsole
{
    void addCommand( std::string command, void* fn );
    ...
}

and in my class I have this function:

void myApp::TestFn( const std::vector<std::string> & args )
{
    // do something
}

in the same class I call this:

void myApp::initApp( )
{
    myConsole::getSingleton( ).addCommand( "FirstTest", &myApp::TestFn );
}

but this gives me this error:

error c2664 cannot convert parameter 2 from 'void(__thiscall
myApp::*)(const std::vector<_Ty>&)' to 'void *'

how can I solve this?

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛夏尉蓝 2024-12-31 22:09:46

你无法解决这个问题。您无法可靠地将函数指针转换为 void * 并返回。

(我建议您重新设计程序并远离 void*;在 C++ 中实际上并不需要它。)

You can't solve this. You can't reliably cast a function pointer to void * and back.

(I suggest you redesign the program and stay clear of void*; there's no real need for it in C++.)

杀お生予夺 2024-12-31 22:09:46

这里的问题是您试图传递一个类方法,因为它是一个 void * 指针。这是不可能的。

正确的方法是使用 void addCommand (std::string, void *) 方法的模板。像这样的东西

class myConsole {
    template <typename T>
    void addCommand( std::string command, T f);
};

struct Callback {
    myApp &app;
    Callback (myApp &a) : app(a) {
    }
    void operator() (const std::vector<std::string> &args) {
        app.TestFn(args);
    }
};

void myApp::initApp( )
{
    myConsole::getSingleton( ).addCommand( "FirstTest", Callback(*this) );
}

给了你C++中的回调原理,但我认为你需要比这个解决方案更灵活的东西,因为你实际上想自动选择回调将执行的命令(在本例中为TestFn)。

The problem here is that you are trying to pass a class method as it were a void * pointer. This cannot be done.

The right way of doing this is by using templates for the void addCommand (std::string, void *) method. Something like

class myConsole {
    template <typename T>
    void addCommand( std::string command, T f);
};

struct Callback {
    myApp &app;
    Callback (myApp &a) : app(a) {
    }
    void operator() (const std::vector<std::string> &args) {
        app.TestFn(args);
    }
};

void myApp::initApp( )
{
    myConsole::getSingleton( ).addCommand( "FirstTest", Callback(*this) );
}

This gives you the callback principle in C++, but I think you need something more flexible than this solution, since you actually want to choose automatically the command that will be executed by the callback (in this case TestFn).

爱你不解释 2024-12-31 22:09:46

您应该避免使用 void*,尤其是在尝试使用函数指针时。我假设您只查看 myApp 类中的成员函数指针,并且您只对采用 const std::vector的成员函数指针感兴趣。 std::string> &args 作为参数。此 typedef 将创建适当的类型并将其命名为 MemFunType

typedef void (myApp :: * MemFunType) (const std::vector<std::string> &);

这是一个完整的示例(位于 ideone),其中有两个不同的成员函数您可能感兴趣,TestFnTestFnBackwards。这个示例可能不是很有用,但它提供了一些成员函数指针的示例。

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

struct myApp;

struct myConsole
{
        typedef void (myApp :: * MemFunType) (const std::vector<std::string> &);
            void addCommand( std::string command, MemFunType fn );
};

struct myApp {
        void TestFn( const std::vector<std::string> & args ) {
                cout << " TestFn" << endl;
                for(std :: vector<std::string> :: const_iterator i = args.begin(); i!=args.end(); i++) {
                        cout << *i << endl;
                }
        }
        void TestFnBackwards( const std::vector<std::string> & args ) {
                cout << " TestFnBackwards" << endl;
                for(std :: vector<std::string> :: const_reverse_iterator i = args.rbegin(); i!=args.rend(); i++) {
                        cout << *i << endl;
                }
        }
        static myApp & getSingleton();
} ma;
myApp& myApp :: getSingleton() {
        return ma;
}

void myConsole :: addCommand( std::string , MemFunType fn ) {
        vector<string> words;
        words.push_back("hello");
        words.push_back("world");
        myApp &ma = myApp :: getSingleton();
        (ma.*fn)(words); // execute the member on the singleton object, using the words as the argument.
}

int main() {
        myConsole m;
        m.addCommand( "FirstTest", &myApp::TestFn );
        m.addCommand( "FirstTest", &myApp::TestFnBackwards );
}

You should avoid void*, especially when trying to use function pointers. I'm going to assume that you are looking only at member-function pointers in the myApp class, and that you are only interested in member-function pointers which take const std::vector<std::string> &args as an argument. This typedef will create the appropriate type and call it MemFunType

typedef void (myApp :: * MemFunType) (const std::vector<std::string> &);

Here is a complete example (on ideone), where there are two different member-functions you may be interested in, TestFn and TestFnBackwards. This example probably isn't very useful, but it gives some examples of member-function pointers.

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

struct myApp;

struct myConsole
{
        typedef void (myApp :: * MemFunType) (const std::vector<std::string> &);
            void addCommand( std::string command, MemFunType fn );
};

struct myApp {
        void TestFn( const std::vector<std::string> & args ) {
                cout << " TestFn" << endl;
                for(std :: vector<std::string> :: const_iterator i = args.begin(); i!=args.end(); i++) {
                        cout << *i << endl;
                }
        }
        void TestFnBackwards( const std::vector<std::string> & args ) {
                cout << " TestFnBackwards" << endl;
                for(std :: vector<std::string> :: const_reverse_iterator i = args.rbegin(); i!=args.rend(); i++) {
                        cout << *i << endl;
                }
        }
        static myApp & getSingleton();
} ma;
myApp& myApp :: getSingleton() {
        return ma;
}

void myConsole :: addCommand( std::string , MemFunType fn ) {
        vector<string> words;
        words.push_back("hello");
        words.push_back("world");
        myApp &ma = myApp :: getSingleton();
        (ma.*fn)(words); // execute the member on the singleton object, using the words as the argument.
}

int main() {
        myConsole m;
        m.addCommand( "FirstTest", &myApp::TestFn );
        m.addCommand( "FirstTest", &myApp::TestFnBackwards );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文