Advertisement

Function pointers as class members

Started by September 15, 2000 09:54 AM
5 comments, last by Yanroy 24 years, 3 months ago
The subject kinda says it all. I want to make a class that a pointer to a function in it. Sort of like customizing polymorphism... Every time I write some code, MSVC++ 6 gives me an error (I think it says unexpected void *)
    
class MyClass
{
public:
    void (*pFunc)(int Value);
};

void main()
{
    MyClass mine;
    mine.pFunc = SomeFuncThatTakesAnInt;
    mine.pFunc(10);
}
    
That is a really stupid example, and I am doing it from memory, so maybe my pointer to a function declaration isn''t right (I copied the real one straight out of my C++ book). A pointless example, but I hope it gets my point across (no pun intended) --------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

All function declarations within a class assume the __thiscall calling style. To point to a function outside of a class it need to explicitly declared as a __stdcall pointer or what ever calling style you are using, try this:
[source]
class MyClass
{
public:
void __stdcall (*ptr)(int Value);
};

MyClass instance;

void _stdcall SomeFunc(int arg)
{
return arg;
}

void main()
{
instance.ptr = SomeFunc;
printf("%d",instance.ptr(1)); // should output ''1''
}

there may be stuff wrong in the source, but the idea is right...


Advertisement
Thanks a lot... I try that ASAP. Now if only people would look at my QTVR thread, I could have a working program before dinner!

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Actually it doesn''t. The second one was wrong, as it returns a VOID, not an INT. You might be trying to return a value, which in that case, you must declare your function to return another type.

I have a perfectly running engine (in fact, the Gauntlet Style Project graphics core) that does what the first person was trying to do (no __stdcall). Maybe it''s in the compiler settings, but I have no problem.

Here''s an example of mine:

    class MyClass{  public:    int (*Func)(int Val);  // Notice the INT};MyClass Mine;int FunctionToCall(int Val);main(){  Mine.Func = FunctionToCall;  printf("%u", Mine.Func(10))}int FunctionToCall(int Val){  return Val;}    



Jim Adams

To my knowledge, any function pointer declared in a class will point to a stdcall function. In order to make a pointer to a member function (as in, one that works in the thiscall convention) you have to declare it like this:

int (ClassName::*pFunc)(int);

If you don''t have the classname, and then the scope resolution operator then it will be a standard func pointer, not a member func pointer.
sort of on the topic...

the first example (the one jim does) also works for my settings... but is there any way to hide the function the pointer points to within the class (as in not defined outside of it)?
___________________________Freeware development:ruinedsoft.com
Advertisement
If you mean you want to call a member function, then you must first declare that function as static, such as:

    class MyClass{  public:    int (*Func)(int Val);    static int MemberFunc(int Val);}int MyClass::MemberFunc(int Val){  return Val}MyClass Mine;main(){  Mine.Func = MyClass::MemberFunc; // or Mine.Memberfunc  printf("%u", Mine.Func());}    



Jim Adams



Edited by - Jim Adams on September 16, 2000 9:29:13 AM

This topic is closed to new replies.

Advertisement