相关疑难解决方法(0)

如何通过需要自由函数的成员函数?

问题如下:考虑这段代码:

#include <iostream>


class aClass
{
public:
    void aTest(int a, int b)
    {
        printf("%d + %d = %d", a, b, a + b);
    }
};

void function1(void (*function)(int, int))
{
    function(1, 1);
}

void test(int a,int b)
{
    printf("%d - %d = %d", a , b , a - b);
}

int main (int argc, const char* argv[])
{
    aClass a();

    function1(&test);
    function1(&aClass::aTest); // <-- How should I point to a's aClass::test function?

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我如何使用a's aClass::test …

c++ arguments function-pointers parameter-passing pointer-to-member

106
推荐指数
6
解决办法
23万
查看次数

通过函数指针调用C++类方法

如何获取类成员函数的函数指针,然后使用特定对象调用该成员函数?我想写:

class Dog : Animal
{
    Dog ();
    void bark ();
}

…
Dog* pDog = new Dog ();
BarkFunction pBark = &Dog::bark;
(*pBark) (pDog);
…
Run Code Online (Sandbox Code Playgroud)

另外,如果可能的话,我也想通过指针调用构造函数:

NewAnimalFunction pNew = &Dog::Dog;
Animal* pAnimal = (*pNew)();    
Run Code Online (Sandbox Code Playgroud)

这是可能的,如果是这样,那么首选的方法是什么?

c++ function-pointers class-method

105
推荐指数
8
解决办法
26万
查看次数

模板类的多变量参数包

我正在使用可变参数包进行基于策略的类设计.

template <APITypes APIType, class... Policies>
class IShader : public Policies... {

};
Run Code Online (Sandbox Code Playgroud)

调用时会定义策略,如果未指定,则使用默认值定义策略.当我需要添加另一个可变参数包时,问题出现了:

template <AttributeType... Attributes, APITypes APIType, class... Policies>
class IShader : public Policies... {

};
Run Code Online (Sandbox Code Playgroud)

这会导致错误"模板参数包必须是最后一个模板参数".我打算使用属性包来更改至少一个策略的行为.但我无法弄清楚如何在一个模板类中获得两个可变参数包.

c++ variadic-templates c++14

8
推荐指数
2
解决办法
2395
查看次数

将任何泛型函数作为 C++ 参数传递

我试图将任何泛型函数作为 C++ 函数中的参数传递。作为额外的乐趣,该函数将驻留在其自己的单独类中。在这种特殊情况下,它们都将不带任何参数并返回void。这包括任何泛型类的成员。

我当前的代码如下所示:

class functionsFedHere {
public:
    void (*callback)();
    functionsFedHere(void(*)());
}

functionsFedHere::functionsFedHere (void(*callbackFunction)()) {
    callback = callbackFunction;
}

void fn1() { cout<<"Fn1"<<endl; }

class myClass {
public:
    int i;
    void fn2() { cout<<i<<endl; }
}

class myOtherClass {
public:
    string j;
    void fn3() { cout<<j<<endl; }
}

int main() {

    // Initialise some objects
    myClass b;
    b.i = 2;

    myOtherClass c;
    c.j = "some text";

    // Works fine with non-member functions
    functionsFedHere objectA(fn1);
    objectA.callback();

    // Doesn't work …
Run Code Online (Sandbox Code Playgroud)

c++ pointers arguments function member

2
推荐指数
1
解决办法
3348
查看次数

如何将成员函数作为参数传递?

C++语法正在扼杀我.我正在尝试将this+指针传递给成员函数:所以我做了以下事情:

template <void(Myclass::*func)()>
static void Myfunction(Myclass* theThis)
{
    theThis->*func();
}
Run Code Online (Sandbox Code Playgroud)

这非常有效.

但是现在我想从这个函数传递给另一个函数这个成员函数.

template <void(Myclass::*func)()>
static void Myfunction2(Myclass* theThis) // My new function
{
    theThis->*func();
}

template <void(Myclass::*func)()>
static void Myfunction(Myclass* theThis)
{
    Myfunction2<&(Myclass::*func)>(theThis)  // This doesn't compile, the template parameter is probably incorrect
}
Run Code Online (Sandbox Code Playgroud)

但它不编译,我不知道如何传递这个成员函数.

我明白了: error C2059: syntax error: '<tag>::*'

编辑:

只是为了说清楚.我没有名为func的函数,这只是指向成员函数的指针的名称

c++ templates

2
推荐指数
1
解决办法
2115
查看次数

C++ 指向成员函数的函数指针

我有一个工作类,它在构造函数中接受回调函数指针初始化。我想在我的类中实例化worker,并提供一个指向成员函数的指针作为回调。

工人阶级:

class Worker
{
public:
    typedef int (*callFunct)(int);
    Worker(callFunct callback = nullptr);
    ~Worker();

private:
    callFunct m_callback = nullptr;
};

Worker::Worker(callFunct callback) : m_callback(callback)
{
}
Run Code Online (Sandbox Code Playgroud)

这是我的类,我希望在其中有Worker实例和成员回调函数:

class myClass
{
public:
    myClass();
    ~myClass() = default;

private:
    int myCallback(int x);
    Worker m_worker {&myClass::myCallback};
};
Run Code Online (Sandbox Code Playgroud)

不幸的是这不能编译:

    error: could not convert '{&myClass::myCallback}' from '<brace-enclosed initializer list>' to 'Worker'
Run Code Online (Sandbox Code Playgroud)

我知道在处理指向成员类的指针时,我还必须提供实例对象。但是,当我需要指向类成员的指针时,在这种情况下正确的做法是什么?

c++

0
推荐指数
1
解决办法
120
查看次数

函数指针指向成员函数

我正在研究优化算法,在我的算法中,我需要使用函数指针将函数传递给优化器

类定义:

class Downhill
{
public: //
    Downhill(std::string fpath);
    ~Downhill();
    void run();
private:
    /*objfun will be passed to amoeba function by function pointer*/
    float objfun1(float *p,int ndim);
    float objfun2(float *p,int ndim);
    float objfun3(float *p,int ndim);
    void amoeba(float **p, float pval[], int ndim, float(*funk)(float *, int ndim), float ftol, int &niter);
  
private:

    float ftol;
    float TINY = 1E-10;
    int NMAX = 5000;
    std::ofstream fout;
};

Run Code Online (Sandbox Code Playgroud)

在成员函数 run() 的声明中,我做了如下操作:

float(*funk)(float *p, int dim);
funk = &(this->objfun1); // I define the function pointer …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
87
查看次数