C ++从函数定义在main中的类中的函数指针的向量调用函数 [英] C++ calling a function from a vector of function pointers inside a class where the function definition is in main

查看:384
本文介绍了C ++从函数定义在main中的类中的函数指针的向量调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,在我的主要有:

  void somefunction 
int main()
{
// bla bla bla
SomeClass myclass = SomeClass();
void(* pointerfunc)()= somefunction;
myclass.addThingy(pointerfunc);

//后来我做
myclass.actionWithDiffrentOutcomes();
}

void somefunction()
{
//一些代码
}

和类中:

  class SomeClass b $ {
public:
void addThingy(void(* function)());
void actionWithDiffrentOutcomes();
private:
std :: vector< void(**)()>向量函数;
}
SomeClass :: addThingy(void(* function)())
{
vectoroffunctions.push_back(& function);
}
SomeClass :: actionWithDiffrentOutcomes()
{
(* vectoroffunctions [0])();;
}

我是一个新的指针, c ++书,googled,ext。这看起来是正确的,编译,运行,但是当我调用actionWithDiffrentOutcomes()我得到访问冲突。我不知道该怎么办。它似乎是正确的,但有些事显然是错误的。那么当定义在另一个类中时,如何从一个类中调用一个函数?



我这样做,因为我不能硬编码每个选项到一个switch语句。

解决方案

您的代码几乎是正确的。你的向量错误地拿着指向函数的指针,而不是简单地指向函数的指针。 addThingy 正在将函数的地址添加到向量

<$>

更改您的代码,如下所示:



<$> p $ p> //存储指向函数的指针,而不是
//指向函数指针的指针
std :: vector< void(*)()>向量函数;

SomeClass :: addThingy(void(* function)())
{
//不要使用地址的地址:
vectoroffunctions.push_back功能);
}

此外,在其余代码中有很多语法错误应该停止代码甚至编译。


Alright, in my main i have:

void somefunction();
int main()
{
    //bla bla bla
    SomeClass myclass = SomeClass();
    void(*pointerfunc)() = somefunction;
    myclass.addThingy(pointerfunc);

    //then later i do
    myclass.actionWithDiffrentOutcomes();
}

void somefunction()
{
    //some code
}

and in the class:

class SomeClass()
{
    public:
        void addThingy(void (*function)());
        void actionWithDiffrentOutcomes();
    private:
        std::vector<void (**)()> vectoroffunctions;
}
SomeClass::addThingy(void (*function)())
{
    vectoroffunctions.push_back(&function);
}
SomeClass::actionWithDiffrentOutcomes()
{
    (*vectoroffunctions[0])();;
}

I'm sort of new-ish to pointers, but I read over my c++ books, googled, ext. and this seems correct, compiles, runs but when I call "actionWithDiffrentOutcomes()" I get an access violation. I'm not sure what to do. it seems correct, but something is obviously wrong. So how can I call a function from within a class when the definition is in another?

I'm doing it this way because i cannot hard-code every option into a switch statement.

解决方案

Your code is almost correct. Your vector is mistakenly holding pointers to pointers to functions rather than simply pointers to functions. addThingy is adding the address of the function pointer in to the vector, but that pointer goes out of scope in the next line.

Change your code as follows:

//Store pointers to functions, rather than
//pointers to pointers to functions
std::vector<void (*)()> vectoroffunctions;

SomeClass::addThingy(void (*function)())
{
    //Don't take the address of the address:
    vectoroffunctions.push_back(function);
}

Also, you have a lot of syntax errors in the rest of the code which should stop the code from even compiling.

这篇关于C ++从函数定义在main中的类中的函数指针的向量调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆