Jump to content
  • Advertisement

Archived

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

_DarkWIng_

function pointers & classes

This topic is 5792 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''ve got a class and one of the functions takes as a parameter a function pointer. This works just fine if that function is some global one. But if it''s a member of another class the it just doesn''t work. Any idea how to solve this? class Cubemap { void RenderToCubemap( void (*renderFunction)(void) ); ... } this works: Cubemap cm cm.RenderToCubemap( someGlobalFunction ); but this doesn''t cm.RenderToCubemap( someOtherClass.publicFunction ); You should never let your fears become the boundaries of your dreams.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by _DarkWIng_
I''ve got a class and one of the functions takes as a parameter a function pointer. This works just fine if that function is some global one. But if it''s a member of another class the it just doesn''t work. Any idea how to solve this?


class Cubemap {
void RenderToCubemap( void (*renderFunction)(void) );
...
}

this works:
Cubemap cm
cm.RenderToCubemap( someGlobalFunction );

but this doesn''t
cm.RenderToCubemap( someOtherClass.publicFunction );



Type safety -- it''s the same reason you can''t pass a function with different parameters or that returns a value. It doesn''t match your declaration. Think about when you use a nonstatic member function of a class -- when you call it, you are implicitly passing the object you are using the function on to the member function (it''s just hidden from you).

A.MyMethod()

may look like it takes no parameters, but internally it''s passing the address of the object A to the function.

If you were to attempt to store the address of a member function to a global function pointer, just think of the problems you''d have -- you wouldn''t be able to associate the call with an object.

They are two completely different types of pointers, and you can''t directly convert between the two. If you want a pointer to a member function use the syntax

Returntype ( YourClasstype::*MethodPointerName )( explicit parameters );

Share this post


Link to post
Share on other sites
Stoffel : TNX, but I already read that page and stuff describe there just doesn''t work for me.
Matt Calabrese : If I understand this right, I would have to create a function for every diferent class/function that has to call this function, or am i totaly off here? Would you be so nice and write a quick example?

You should never let your fears become the boundaries of your dreams.

Share this post


Link to post
Share on other sites
Not for every method, just every method type, just like with functions. Here're some examples. If you need more to understand, just ask:


  class MyClass1
{
public:
void DoSomething() const;
int DoSomethingElse() const;
};

class MyClass2
{
public:
void DoSomething() const;
};

int main()
{
void (MyClass1::*SomeMethod)() const = MyClass1::DoSomething; // Will work

void (MyClass1::*SomeMethod)() const = MyClass2::DoSomething; // Will not work


void (MyClass1::*SomeMethod)() const = MyClass1::DoSomethingElse; // Will not work


return 0;
}


[edited by - Matt Calabrese on November 8, 2002 3:27:51 PM]

Share this post


Link to post
Share on other sites
This is something I''ve been confused about. Am I right in thinking that the member function pointer can only be called from another member function?

Share this post


Link to post
Share on other sites
No, but a member function pointer needs to have an object in order to be called. The difference between free functions and member functions are that the member functions have an "invisible" parameter called "this" passed along to it. It''s just that instead of passing it as an argument:
memFun (&object);
you pass it by "calling" the member on that object:
object.memFun ();

So if you use a member function pointer, you have to have an object and use the pointer-to-member operator:
(object.*memFunPtr) ();

Share this post


Link to post
Share on other sites
This comes up every other week, doesn''t it? I wish the search were back up (hint: use Google with "site:gamedev.net").

Share this post


Link to post
Share on other sites
OK, I understand now why I need ClassName::*functionName() stuff but the thing is it still doesn''t work. I just get different error.


Class B {
Call( void (*callie)(void) ) {
callie();
)
}

Class A {
void Render() {
// do some stuff here
}
void Play() {
// 1) original idea
instanceOfB.Call( Render );
// 2) idea from www.function-pointer.org
instanceOfB.Call( A::*Render );
// 3) from what all of you wrote
void (A::*myFunction)() = A::Render;
instanceOfB.Call( myFunction );
}
B instanceOfB;
}


What do I need to rewrite to make this work?


You should never let your fears become the boundaries of your dreams.

Share this post


Link to post
Share on other sites
quote:
Original post by _DarkWIng_
OK, I understand now why I need ClassName::*functionName() stuff but the thing is it still doesn''t work.

Actually, you apparently don''t understand. Using your example, for B::Call to be able to accept one of A''s methods its argument (callie) would need to be of type (void A:: *)(void), not (void *)(void). No amount of syntactic gymnastics will alter that requirement.

Besides, what does A need B for? It can maintain a pointer to any of its own functions and call them directly. What is the real "problem" you''re trying to solve?

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!