• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# C++ and functions as variables

## Recommended Posts

Hello, I got a little problem regarding C++ and the use of variables representing functions. I have the following two classes:
  // file: ClassB.h #ifndef CLASSB_H_INCLUDED #define CLASSB_H_INCLUDED // defenition for the function typedef int (*FUNCTION)(int x, int y); class ClassB { public: void SetFunction(FUNCTION func); void DoSomething(); private: FUNCTION func; }; #endif // CLASSB_H_INCLUDED 
  // file: ClassA.h #ifndef CLASSA_H_INCLUDED #define CLASSA_H_INCLUDED #include "ClassB.h" class ClassA { public: ClassA(); TheFunction(int x, int y); }; #endif // CLASSA_H_INCLUDED 
  // file: ClassB.cpp #include "ClassB.h" // sets the function variable void ClassB::SetFunction(FUNCTION func) { this->func = func; } // calls our function variable void ClassB::DoSomething() { func(5,5); } 
  // file: ClassA.cpp #include "ClassA.h" ClassA::ClassA() { // initialize ClassB and set the function to TheFunction ClassB classB; // here is the problem classB.SetFunction(TheFunction); classB.DoSomething(); } // the actual function int ClassA::TheFunction(int x, int y) { return(x + y); } 
I hope you understand what I''m trying to do here. ClassB has a variable which contains a function. This variable is assigned with the public function TheFunction of ClassA. The assignment occurs with the method ClassB.SetFunction. That''s were the compiler gives me an error. It says more or less the following: cannot convert parameter 1 from void (int x, int y) to void (__cdecl *)(int x, int y) Can anyone tell me what the problem is? Because when I move the method TheFunction out of ClassA (like this: int TheFunction and not int ClassA::TheFunction) it works perferctly. So the problem must be somewhere that TheFunction is a member function and not a standard function.

##### Share on other sites
The problem is with the function calling convention. The default calling convention is __cdecl (which is what your function pointer is being declared as) and the calling convention for your member function is __thiscall (as are all member functions)
you have two options....

1. Make TheFunction() static in class A

2. Change your declaration of the FUNCTION to this (I think this is right)

typedef int (*A::FUNCTION)(int x,int y);

##### Share on other sites
I'm not going to make it static because the method will be using member variables and I'm not planning to make them all static.
So, I'll try the second option you mentioned. I hope it's right.
Thanks anyway.

Oh yeah, forgot to ask: the A in (*A::FUNCTION)..., does it refer to the name of the class or is it some special thing that makes it work?

Edited by - soehrimnir on June 12, 2001 12:07:00 PM

##### Share on other sites
The A refers to the name of the class. It should have been ClassA really (my mistake)

Note that that solution will work for the example you have given, but it will only *ever* allow the function pointer to point to functions which are members of ClassA.

eg
  class ClassC{public: ClassC(); TheOtherFunction(int x, int y);};// somewhere in a cpp file...B.SetFunction(A.TheFunction); // this worksB.SetFunction(C.TheOtherFunction); // this doesnt work

Edited by - Sandman on June 12, 2001 12:20:28 PM

##### Share on other sites
Jus'' pass in the class pointer and make the function static.

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978705

• 21
• 14
• 12
• 10
• 12