Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用C++;使用函数模板类成员函数_C++_Templates_C++11 - Fatal编程技术网

C++ 调用C++;使用函数模板类成员函数

C++ 调用C++;使用函数模板类成员函数,c++,templates,c++11,C++,Templates,C++11,我希望能够使用某种函数模板在类之外调用成员函数(例如类testClass中的doSomething())。简言之,可以用这种方式调用非静态成员函数,functionCaller(t.doSomething)frommain()

我希望能够使用某种函数模板在类之外调用成员函数(例如类
testClass
中的
doSomething()
)。简言之,可以用这种方式调用非静态成员函数,
functionCaller(t.doSomething)
from
main()

<我有一个C++类,比如:

class testClass {
public:
    testClass()
    {
        value = 0;
    }
    int prod(int i)
    {
        value *= i;
        return value;
    }
    int doSomething()
    {
        value = prod(10);
        return value;
    }

private:
    int value;
};
我的功能模板如下所示:

template<typename T, typename ret>
ret callFunction(T t, ret (T::*func)()) {

    // checkSomePermissionsHere()
    return t.func()
}
#include <functional>
#include <iostream>

class EventHandler
{
    public:
        void addHandler(std::function<void()> callback)
        {
            std::cout << "Handler added..." << std::endl;
            // Let's pretend an event just occured
            callback();
        }
};

class testClass {
public:
    testClass()
    {
        value = 0;
        EventHandler handler;
        handler.addHandler(std::bind(&testClass::doSomething, this));
    }
    int doSomething()
    {
        value = prod(10);
        std::cout << "I did something!\n";
        return value;
    }

private:
    int value;
};

int main()
{
    testClass t1;
    return 0;
}
我得到这个错误:

error: no member named 'func' in 'testClass'
return t.func(a);
         ^
通过
callFunction
方法对对象
t1
调用此成员函数(
doSomething
)的正确方法是什么?我想实现
callFunction
,作为通用API的一部分,该API在执行提供的功能之前进行一些检查

我正在为我的编译器使用clang/LLVM 3.5。

更改此选项:

return t.func()
为此:

return (t.*func)();
它应该编译并执行良好


正如Joachim Pileborg所说,你可以使用和。在这种情况下,您的代码可能如下所示:

template<typename T, typename ret>
ret callFunction(T t, ret (T::*func)()) {

    // checkSomePermissionsHere()
    return t.func()
}
#include <functional>
#include <iostream>

class EventHandler
{
    public:
        void addHandler(std::function<void()> callback)
        {
            std::cout << "Handler added..." << std::endl;
            // Let's pretend an event just occured
            callback();
        }
};

class testClass {
public:
    testClass()
    {
        value = 0;
        EventHandler handler;
        handler.addHandler(std::bind(&testClass::doSomething, this));
    }
    int doSomething()
    {
        value = prod(10);
        std::cout << "I did something!\n";
        return value;
    }

private:
    int value;
};

int main()
{
    testClass t1;
    return 0;
}
#包括
#包括
类事件处理程序
{
公众:
void addHandler(std::函数回调)
{

std::cout
return(t.*func)()
Ahh…谢谢。这很有效。你可能会对这些问题感兴趣,而且,这会使处理这些问题变得更容易。这真的很好@JoachimPileborg,你能扩展一点吗?如果我理解得很好,我可能能够改进我的答案!:D@gsamaras参见,例如,我不确定我是否有权访问functional。你知道clang 3.5是否支持它吗但是?否则std::functon会是个好主意。谢谢你的建议。编辑:我知道它已经被支持了一段时间了。谢谢你让我们知道@AdwaitDongare!