Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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

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++_Templates_Wrapper - Fatal编程技术网

C++ 通过一门课程';回调函数中的成员函数?

C++ 通过一门课程';回调函数中的成员函数?,c++,templates,wrapper,C++,Templates,Wrapper,我有这个模板: template <class T> v8::Handle<v8::Value> jsFunctionTemplate(const v8::Arguments &args) { T *t = static_cast<T*>(args.This()->GetPointerFromInternalField(0)); if (t != NULL) t->volume(args[0]->NumberValue

我有这个模板:

template <class T>
v8::Handle<v8::Value> jsFunctionTemplate(const v8::Arguments &args)
{
    T *t = static_cast<T*>(args.This()->GetPointerFromInternalField(0));
    if (t != NULL) t->volume(args[0]->NumberValue());
    return args.This();
}
因此,它的用法仍然可以是:

audio->PrototypeTemplate()->Set("Volume", v8::FunctionTemplate::New(&jsFunctionTemplate<Audio>));
audio->PrototypeTemplate()->Set(“Volume”,v8::FunctionTemplate::New(&jsFunctionTemplate));

我甚至不反对使用C++11语法。

我并不完全理解v8 API在这里施加的限制,但是如果类型的模板参数没有问题,那么我希望您可以使用指向成员模板参数的指针来实现您在这里想要实现的目标。以下独立且经过测试的示例(虽然没有v8)应演示如何实现这一点:

#include <iostream>

struct foo {
  void bar(int arg) {
    std::cout << "bar(" << arg << ") called" << std::endl;
  }
};

template<class T, void (T::*fun)(int)>
void ptfWrapup(void* ptr, int arg) {
  (static_cast<T*>(ptr)->*fun)(arg);
}

int main() {
  foo f;
  ptfWrapup<foo, &foo::bar>(&f, 42);
}

与上面的示例相反,这里的代码未经测试,但另一方面更接近您的代码。我希望,它们结合在一起会使这个想法非常清晰。

我并不声称完全理解v8 API在这里施加的限制,但是如果类型的模板参数没有问题,那么我希望您可以使用指向成员模板参数的指针来实现您在这里想要实现的目标。以下独立且经过测试的示例(虽然没有v8)应演示如何实现这一点:

#include <iostream>

struct foo {
  void bar(int arg) {
    std::cout << "bar(" << arg << ") called" << std::endl;
  }
};

template<class T, void (T::*fun)(int)>
void ptfWrapup(void* ptr, int arg) {
  (static_cast<T*>(ptr)->*fun)(arg);
}

int main() {
  foo f;
  ptfWrapup<foo, &foo::bar>(&f, 42);
}
与上面的示例相反,这里的代码未经测试,但另一方面更接近您的代码。我希望,他们的共同努力会使这个想法非常清晰

template <class T, void (T::*fun)(NumberType)>
v8::Handle<v8::Value> jsFunctionTemplate(const v8::Arguments &args)
{
    T *t = static_cast<T*>(args.This()->GetPointerFromInternalField(0));
    if (t != NULL) (t->*fun)(args[0]->NumberValue());
    return args.This();
}

… v8::FunctionTemplate::New(&jsFunctionTemplate<Audio, &Audio::volume>) …