Archived

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

CGameProgrammer

Pointers to functions *within a class*

Recommended Posts

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( );

Share this post


Link to post
Share on other sites
Peter Svensson    122
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!

Share this post


Link to post
Share on other sites
Peter Svensson    122
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!

Share this post


Link to post
Share on other sites
Peter Svensson    122
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!

Share this post


Link to post
Share on other sites
JoeDark    122
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

Share this post


Link to post
Share on other sites
xeos    122
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

Share this post


Link to post
Share on other sites