Member function pointer

Moderator
Posts: 133
Joined: 2008.05
Post: #1
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?
Quote this message in a reply
Member
Posts: 89
Joined: 2005.06
Post: #2
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....
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
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.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #4
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!
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #5
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
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #6
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.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #7
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".
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Pascal language (pointer) problem sealfin 3 4,393 Nov 27, 2012 04:38 AM
Last Post: sealfin
  another c pointer question NelsonMandella 3 3,883 Mar 26, 2010 03:49 AM
Last Post: DoG
  Adding Member Variables to Classes FlamingHairball 2 3,217 Jan 12, 2008 02:43 PM
Last Post: FlamingHairball
  Diferences between array and pointer initialized strings in C ferum 16 6,754 Jan 4, 2006 11:38 PM
Last Post: kelvin
  Cocoa Method to C Pointer KiroNeem 11 6,704 Sep 12, 2005 10:08 AM
Last Post: Zekaric