iDevGames Forums
Member function pointer - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Member function pointer (/thread-4851.html)



Member function pointer - longjumper - Oct 30, 2005 10:10 PM

Excuse my bad memory, but I forgot the syntax.

I have a class that has this definition in it...
Code:
class WorldObject
{
...
void (WorldObject::*draw)();
void drawCube();
};

When I initialize an instance of a WorldObject, I assign drawCube to draw.
Code:
draw = &WorldObject:drawCube;

When I want to draw, I call:
Code:
WorldObject * wo = &(worldObjects[i]);
((WorldObject*)wo->*draw)();

I get a syntax error, saying that it is of unknown type when I try and call the draw function. What gives?


Member function pointer - Volte - Oct 30, 2005 10:30 PM

um... why the heck are you assigning your methods to eachother? First of all, thats not how you declare a function in a class.
class WorldObject {
...
void drawCube (...);
void draw(...);
...
}

if you want drawCube outside of your class you have to declare it outside of your class, otherwise its been declared in your class. This all should be in your header, then in your cpp file you should type

void WorldObject::drawCube(...) {
...
}
and if you want draw to be there, not part of the class because you didnt declare it as part of the class, type:

void drawCube(...) {
..
}

also when you are calling fucntions of a class you dont need to type cast the object because it is already of that type.
Just say
WorldObject myObject;
or
WorldObject myObject = WorldObject(this_is_your_constructor_right_here);
and then
myObject.drawCube(...);

Hope that helps, if not, well I guess this will have to do:
[Image: STEAK.JPG]
Edit: mmmm.... steak....


Member function pointer - OneSadCookie - Oct 30, 2005 10:40 PM

The rule about pointers to member functions is that if you're using them, you're doing something wrong Wink

Unless I'm very much mistaken, this is what inheritance is for.


Member function pointer - TomorrowPlusX - Oct 31, 2005 05:14 AM

OSC is right. There's no *right* way to do this.

Instead, make the draw() method on WorldObject be a pure virtual, and make a CubeWorldObject which overrides draw() to draw a cube, as you like.

Or, make WorldObject a proxy for drawing something else. So you might have something like:

Code:
WorldObject *wo = new WorldObject;
wo->assignVisual( new CubeVisual );

And so on.

There's a lot of "right" ways to do this, but reassigning member functions isn't one of them!


Member function pointer - longjumper - Oct 31, 2005 12:09 PM

Well, in a usual case I would do that. Unfortunately, I have to move this project back and forth between a Windows lab computer and my home computer, extra files(which are required, because the professor is anal, and since I would have to make up to 10 subclasses, that's a lot of really slow uploading) are a huge pain in the ass. If I was writing good code, I would do that, but this is silly crap code. Either way, I figured it out. Smile


Member function pointer - phydeaux - Oct 31, 2005 03:17 PM

LongJumper Wrote:Well, in a usual case I would do that. Unfortunately, I have to move this project back and forth between a Windows lab computer and my home computer, extra files(which are required, because the professor is anal, and since I would have to make up to 10 subclasses, that's a lot of really slow uploading) are a huge pain in the ass. If I was writing good code, I would do that, but this is silly crap code. Either way, I figured it out. Smile

Not really addressed to you in particular, but if you ask a question, and then figure out a solution, can you please post the solution you used? There's nothing more frustrating than searching for a question, and then finding "nevermind I solved it" without explaining how.


Member function pointer - longjumper - Oct 31, 2005 05:53 PM

I figured since this was a no-no, that I wouldn't. But here it is:

The syntax only works if you are within a member function, so I created another function that called the same draw code, expect it was referencing "this" as opposed to "wo".