Jump to content
  • Advertisement

Archived

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

Obsidian Kindragon

Function Pointer problem. Need help with OOP.

This topic is 5883 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

Hello, I''m still somewhat new to programming C++ an dI''m having a bit of trouble with Function Pointers. It''s best if I show an example: struct Painter { Painter(); virtual ~Painter(); BOOL Paint_16(); BOOL Paint_24(); BOOL Paint_32(); BOOL (*Paint)(); }; Whenever I try to assign the (*Paint) pointer to point to one of the other ''Paint'' functions it gives me an error that I''m trying to mix two incompatible pointers. Example: Painter *Artist = new Painter; Artist->Paint = Artist->Paint_32; I can get the pointers to work if I rewrite the structure like this: struct Painter { Painter(); virtual ~Painter(); BOOL Paint_16(); BOOL Paint_24(); BOOL Paint_32(); BOOL (Painter::*Paint)(); }; And then assign the function pointer to point to one of the other paint functions: Painter *Artist = new Painter; Artist->Paint = Artist->Paint_32; But then I still cannot use the (*Paint) function: Artist->Paint(); It gives me an error saying that the ''term does not evaluate to a function''. I can use the function pointers perfectly well if I keep the function pointer and the function the pointer points to out of the structure entirely, but is there any way to use Function Pointers within a structure? Any help would be greatly appreciated. - Obsidian Kindragon

Share this post


Link to post
Share on other sites
Advertisement
I thought you couldn''t have function or function pointers, for that matter, in a struct, only member variables. Classes are for those things (functions). Right?

Share this post


Link to post
Share on other sites
quote:
Original post by Alpha_ProgDes
I thought you couldn''t have function or function pointers, for that matter, in a struct, only member variables. Classes are for those things (functions). Right?


In C++, a struct is just a class with the default access set to public, instead of private.

So in C++, a struct can do anything a class can do.

Share this post


Link to post
Share on other sites
Wrong Alpha. You should be able to place function pointers and functions into a structure (as long as you''re using C++).

Anyway, I can''t find the problem in the code posted. Don''t have much experience myself with function pointers. Perhaps http://www.function-pointer.org could help.

Share this post


Link to post
Share on other sites
Try it like this:


struct Painter
{
Painter();
virtual ~Painter();
BOOL __cdecl Paint_16();
BOOL __cdecl Paint_24();
BOOL __cdecl Paint_32();
BOOL __cdecl (*Paint)();
};

And call Paint like this:

Artist.*Paint();

EDIT:

That may need to be
BOOL __cdecl (Painter::*Paint)();
instead of
BOOL __cdecl (*Paint)();


[edited by - TravisWells on September 10, 2002 3:47:40 PM]

Share this post


Link to post
Share on other sites
struct Painter
{
Painter();
virtual ~Painter();
BOOL Paint_16();
BOOL Paint_24();
BOOL Paint_32();
BOOL (*Paint)();
};

Painter Start, *pArtist;


Painter *Artist = new Painter;
pArtist->Paint = Start.Paint_32( );


.... i''m not exactly i just through this out... maybe it might give you an idea.

Share this post


Link to post
Share on other sites
I found out that I can access the (Painter::*Paint) function within other memebr functions of the class/structure. All I need to call the function is this:

(*this.*Paint)();

I still have the problem of not being able to us the (Painter::*Paint) function outside of the class/structure, but I think if I wasn't usign the 'Artist' as a pointer to a Painter then I would access the (Painter::*Paint) function outside of the class/structure like this:

Painter Artist;
Artist.Paint = Artist.Paint_32;
(Artist.*Paint)();

Or so the site Melraidin linked us to leads me to believe. But with my using the Artist as only a pointer to a Painter Object that doesn't seem to work. Neither does this:

Painter *Artist = new Painter;
Artist->Paint = Artist->Paint_32;
(Artist->*Paint)();

Or this:

(*Artist.*Paint)();

Which I thought one of which would a valid reference to the function pointer.

[edited by - Obsidian Kindragon on September 10, 2002 4:10:45 PM]

Share this post


Link to post
Share on other sites
Why not derive a class from Painter for each type of Paint function (16, 24 and 32), make Paint virtual.

Of course, you can''t change the function at runtime unless you recreate the object. But that shouldn''t be a problem.

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com

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!