function-pointers

Can't find my syntax error, VC++ says there's one

I'm running into a bit of a problem here, I'm messing around with machine code and function pointers, and there's a bit of my code that VC++ simply refuses to compile. This compiles and runs exactly as expected: #include <stdlib.h> #include <stdio.h> int main() { char tarr[] = {0xb8, 222, 0, 0, 0, 0xc3}; int (*testfn)() = tar...

Calling a member function from a member function templated argument

Given the following code which I can't get to compile. template < typename OT, typename KT, KT (OT::* KM)() const > class X { public: KT mfn( const OT & obj ) { return obj.*(KM)(); // Error here. } }; class O { public: int func() const { ...

Typedeffing a function (NOT a function pointer)

typedef void int_void(int); int_void is a function taking an integer and returning nothing. My question is: can it be used "alone", without a pointer? That is, is it possible to use it as simply int_void and not int_void*? typedef void int_void(int); int_void test; This code compiles. But can test be somehow used or assigned to som...

Callback functions in c++

In c++, when and how to use a callback function? EDIT: I would like to see a simple example to write a callback function. ...

Why is it not possible to store a function pointer of a base class?

The following code gives an compilation error for void b() { m = &A::a; }; stating that A::a() is protected. (Which it is - but that should be no problem) However the compiler doesn't care when I write B::a(). Even though both mean the same I would prefer A::a() because it states explicitely that a() is defined in A. So what is the reas...

Comparing address of std::endl

I am inspecting a piece of existing code and found out it behaves differently when compiled with Visual C++ 9 and MinGW: inline LogMsg& LogMsg::operator<<(std::ostream& (*p_manip)(std::ostream&) ) { if ( p_manip == static_cast< std::ostream& (*)(std::ostream&) > ( &std::endl<char, std::char_traits<char> >) ) { msg(m_outp...

Javascript Function-Pointer Assignment

Consider this javascript code: var bar = function () { alert("A"); } var foo = bar; bar = function () { alert("B"); }; foo(); When running this code I get "A". Is this behavior a part of javascript specification and can I rely on it? ...

Change the address of a member function in C++

Hello, in C++, I can easily create a function pointer by taking the address of a member function. However, is it possible to change the address of that local function? I.e. say I have funcA() and funcB() in the same class, defined differently. I'm looking to change the address of funcA() to that of funcB(), such that at run time callin...

Passing member function pointer to member object in c++

I have a problem with using a pointer to function in C++. Here is my example: #include <iostream> using namespace std; class bar { public: void (*funcP)(); }; class foo { public: bar myBar; void hello(){cout << "hello" << endl;}; }; void byebye() { cout << "bye" << endl; } int main() { foo testFoo; testFoo...

How to call a pointer to method from another method

I had this problem some time ago and I gave up but lately it returned. #include <iostream> class element2D; class node2D { public: void (element2D::*FunctionPtr)(); void otherMethod() { std::cout << "hello" << std::endl; ((this)->*(this->FunctionPtr))(); //ERROR<------------------- } }; class element2D { publ...

Function pointers and unknown number of arguments in C++

I came across the following weird chunk of code.Imagine you have the following typedef: typedef int (*MyFunctionPointer)(int param_1, int param_2); And then , in a function , we are trying to run a function from a DLL in the following way: LPCWSTR DllFileName; //Path to the dll stored here LPCSTR _FunctionName; // (mangled) name...

Executing functions depending on a variable's value - C++

I am working on a transliteration tool. I have two modules lexer and translator. Lexer generates tokens out of the input text. Depending on the current language chosen, I have to call appropriate translation routine. I have came up with couple of ideas to do this. First one is to create a base class something called base_translator and ...

C++, function pointer to member function

I'd like to set up a function pointer as a member of a class that is a pointer to another function in the same class. The reasons why I'm doing this are complicated. In this example, I would like the output to be "1" class A { public: int f(); int (*x)(); } int A::f() { return 1; } int main() { A a; a.x = a.f; printf("%d\n",a....

Can someone explain the difference between these two typedefs? (Function pointer related)

This one is quite obviously a function pointer : typedef int (* foobar) (int a, unsigned char *b, unsigned int c); But what does this one do? typedef int (foobar *) (int a, unsigned char *b, unsigned int c); ...

Does Function pointer make the program slow?

Hi guys. I read about function pointers in C. And everyone said that will make my program run slow. Is it true? I made a program to check it. And I got the same results on both cases. (measure the time.) So, is it bad to use function pointer? Thanks in advance. To response for some guys. I said 'run slow' for the time that I have comp...

Generic function pointers in C

I have a function which takes a block of data and the size of the block and a function pointer as argument. Then it iterates over the data and performes a calculation on each element of the data block. The following is the essential outline of what I am doing: int myfunction(int* data, int size, int (*functionAsPointer)(int)){ //wa...

Pointer to a C++ class member function as a global function's parameter?

I have got a problem with calling a global function, which takes a pointer to a function as a parameter. Here is the declaration of the global function: int lmdif ( minpack_func_mn fcn, void *p, int m, int n, double *x, double *fvec, double ftol) The "minpack_func_mn" symbol is a typedef for a pointer to a function, defin...

What type can hold member-function-pointers of difference classes in C++?

Hi all, I need an array to hold member-function-pointers of different classes. How can I define the array? The code should look like this : arr[0] = &CMyClass::FuncX; arr[1] = &CYourClass::FuncY; arr[2] = &CHerClass::FuncZ; I tried void*, but it doesn't work. Thanks. Best regards, Zach@Shine ...

Call c++ function pointer from c#

Is it possible to call a c(++) static function pointer (not a delegate) like this typedef int (*MyCppFunc)(void* SomeObject); from c#? void CallFromCSharp(MyCppFunc funcptr, IntPtr param) { funcptr(param); } I need to be able to callback from c# into some old c++ classes. C++ is managed, but the classes are not ref classes (yet)....

Misunderstanding function pointer - passing it as an argument

I want to pass a member function of class A to class B via a function pointer as argument. Please advise whether this road is leading somewhere and help me fill the pothole. #include <iostream> using namespace std; class A{ public: int dosomeA(int x){ cout<< "doing some A to "<<x <<endl; return(0); } }; class B{ public: B...