Advertisement

Pointers to functions *within a class*

Started by January 26, 2001 04:55 PM
7 comments, last by CGameProgrammer 24 years ago
I need my class to have pointers to other functions of itself. Here''s a simple example:
  
class C
{
    public:

        void (C::*FunctionPtr) ( );
        void FunctionToCall( ) { }
};

void main ( )
{
    C Test;

    Test.FunctionPtr = Test.FunctionToCall; // Compiles fine.

    Test.FunctionPtr( ); // ERROR

}
  
When I try to call FunctionPtr(), the compiler says the term does not evaluate to a function. So, how do I do this? ~CGameProgrammer( );

~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Hi there...

Not sure if this will help but...

I think your code is producing an inline function. If you put the method body outside the class definition it wont become inlined and your code should work!

//Cheers!
Advertisement
Hi there...

Not sure if this will help but...

I think your code is producing an inline function. If you put the method body outside the class definition it wont become inlined and your code should work!

//Cheers!
Hi there...

Not sure if this will help but...

I think your code is producing an inline function. If you put the method body outside the class definition it wont become inlined and your code should work!

//Cheers!
Nice idea, but that didn''t help -- I tried it. I also tried making Test a pointer; didn''t help either.

~CGameProgrammer( );

~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
As far as I know you can't have a regular pointer to a member function, only a pointer-to-member offset of a member function.

Something like this:

      class C{public:   void (C::*FunctionPtr) ( );   void FunctionToCall( ) { }};void main ( ){   C Test;   Test.FunctionPtr = &C::FunctionToCall;   // Compiles fine.   (Test.*Test.FunctionPtr)();}      


This makes *FunctionPtr hold an offset to the function FunctionToCall() for any particular instance of class C.

So the last line says access whatever function is at the offset defined by .*Test.FunctionPtr in the Test object.

JoeDark

Edited by - JoeDark on January 26, 2001 7:48:48 PM

Edited by - JoeDark on January 26, 2001 7:50:14 PM
Advertisement
Twilight''s Prophercy 3D did do it with their 1.01 alpha release in their time class - they selected between a mm timer and a high-performance timer based upon performance on that machine.

Unfortunately, I don''t have the source code any mode. However, you can still find it at http://www.twilight3d.com as pr3dcore100.zip -- the later versions don''t use this feature.

Hope that helps,

Simon Wilson,
XEOS Digital Development
XEOS Digital Development - Supporting the independant and OpenSource game developers!
I also believe that you can have a pointer to a member function if the function is declared static.

JoeDark
It worked the way you said. Thanks, JoeDark.

~CGameProgrammer( );

~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement