Member function pointer
Excuse my bad memory, but I forgot the syntax.
I have a class that has this definition in it...
When I initialize an instance of a WorldObject, I assign drawCube to draw.
When I want to draw, I call:
I get a syntax error, saying that it is of unknown type when I try and call the draw function. What gives?
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?
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]](http://www.westvegas.com/food/STEAK.JPG)
Edit: mmmm.... steak....
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:
Edit: mmmm.... steak....
The rule about pointers to member functions is that if you're using them, you're doing something wrong 
Unless I'm very much mistaken, this is what inheritance is for.

Unless I'm very much mistaken, this is what inheritance is for.
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:
And so on.
There's a lot of "right" ways to do this, but reassigning member functions isn't one of them!
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!
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.
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.
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Pascal language (pointer) problem | sealfin | 3 | 2,533 |
Nov 27, 2012 04:38 AM Last Post: sealfin |
|
| another c pointer question | NelsonMandella | 3 | 3,214 |
Mar 26, 2010 03:49 AM Last Post: DoG |
|
| Adding Member Variables to Classes | FlamingHairball | 2 | 2,776 |
Jan 12, 2008 02:43 PM Last Post: FlamingHairball |
|
| Cocoa Method to C Pointer | KiroNeem | 11 | 5,576 |
Sep 12, 2005 10:08 AM Last Post: Zekaric |
|

