可能重复: C ++,成员函数的函数指针 问题是以下几点:考虑这段代码: #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,ab); } int main (int argc, const char * argv[]) { aClass a(); function1(&test); function1(&aClass::aTest ); // <– how should I point to a's aClass::test […]
我应该如何使用C中的函数指针数组? 我怎样才能初始化它们?
所以我觉得在制作函数指针时,不需要operator &来获取初始函数的地址: #include <stdio.h> double foo (double x){ return x*x; } int main () { double (*fun1)(double) = &foo; double (*fun2)(double) = foo; printf("%f\n",fun1(10)); printf("%f\n",fun2(10)); printf("fun1 = %p \t &foo = %p\n",fun1, &foo); printf("fun2 = %p \t foo = %p\n",fun2, foo); int a[10]; printf(" a = %p \n &a = %p \n",a,&a); return 0; } […]
这可能是一些常见和微不足道的事情,但我似乎无法find具体的答案。 在C#中有一个委托的概念,强烈地涉及到来自C ++的函数指针的思想。 Java中是否有类似的function? 鉴于指针是有点缺席,这是最好的方法是什么? 而要清楚的是,我们在这里讲第一课。
我正在玩C ++ lambda和他们隐式转换为函数指针。 我的起始示例是使用它们作为ftw函数的callback函数。 这按预期工作。 #include <ftw.h> #include <iostream> using namespace std; int main() { auto callback = [](const char *fpath, const struct stat *sb, int typeflag) -> int { cout << fpath << endl; return 0; }; int ret = ftw("/etc", callback, 1); return ret; } 修改后使用捕获: int main() { vector<string> entries; auto callback […]
我正在使用一个API,要求我传递一个函数指针作为callback。 我试图从我的class级使用这个API,但我得到编译错误。 这是我从我的构造函数做的: m_cRedundencyManager->Init(this->RedundencyManagerCallBack); 这不会编译 – 我得到以下错误: 错误8错误C3867:'CLoggersInfra :: RedundencyManagerCallBack':函数调用缺less参数列表; 使用&CLoggersInfra :: RedundencyManagerCallBack创build一个指向成员的指针 我试着build议使用&CLoggersInfra::RedundencyManagerCallBack – 没有为我工作。 对此有何build议/解释? 我正在使用VS2008。 谢谢!!
对于一个类,我想在一个存储std::function对象的map存储一些指向同一类的成员函数的函数指针。 但是这个代码我一开始就失败了: class Foo { public: void doSomething() {} void bindFunction() { // ERROR std::function<void(void)> f = &Foo::doSomething; } }; 我收到error C2064: term does not evaluate to a function taking 0 arguments在xxcallobj error C2064: term does not evaluate to a function taking 0 arguments结合一些奇怪的模板实例化错误。 目前我正在使用Visual Studio 2010/2011在Windows 8上工作,在VS 7上使用VS10工作也会失败。 错误必须基于一些奇怪的C ++规则,我不遵循。 编辑:我不使用提升。 这是MS编译器中集成的C ++ 11。
当我阅读其他人的代码,这些代码有指向具有参数的函数的指针时,我总是有点难过。 我记得,我花了一段时间才弄清了这个定义,同时试图理解一个用C编写的数值algorithm。 那么,你能分享一下你的技巧和想法:如何编写好的指向函数指针的types定义(Do's and Do not's),为什么它们是有用的,以及如何理解别人的工作? 谢谢!
如何获得类成员函数的函数指针,然后使用特定的对象调用该成员函数? 我想写: class Dog : Animal { Dog (); void bark (); } … Dog* pDog = new Dog (); BarkFunction pBark = &Dog::bark; (*pBark) (pDog); … 另外,如果可能的话,我想通过一个指针调用构造函数: NewAnimalFunction pNew = &Dog::Dog; Animal* pAnimal = (*pNew)(); 这是可能的,如果是这样,那么这样做的首选方法是什么?
我有一个约十行代码的方法。 我想创build更多的方法来完成同样的事情,除了一个小的计算,将改变一行代码。 这是传递函数指针来replace那一行的完美应用程序,但Java没有函数指针。 我最好的select是什么?