Jump to content
  • Advertisement

Archived

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

CGameProgrammer

Pointers to functions *within a class*

This topic is 6380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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!

Share this post


Link to post
Share on other sites
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
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
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
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
I also believe that you can have a pointer to a member function if the function is declared static.

JoeDark

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!