Sign in to follow this  
garyfletcher

Overloading non member functions - is it possible?

Recommended Posts

Okay. So I've got the DLL for my game engine, thanks to everyone for all their help with that. Within the engine I have 2 functions used as tags into a game project, well that's the idea. When building the dll I use:
#ifdef BUILDING_DLL
bool gameEntry(SiSE* GE){return true;}
bool gameExit(SiSE* GE){return true;}
#endif
I was hoping that when I compiled my project with the .a and using the dll and without the BUILDING_DLL defined that I would be able to overload these 2 functions and use functions defined in the project..but alas not. The 2 functions are being called constantly causing a loop and hanging the process....can anyone think of a design/method to get around this?

Share this post


Link to post
Share on other sites
Quote:
Original post by garyfletcher
Okay. So I've got the DLL for my game engine, thanks to everyone for all their help with that.

Within the engine I have 2 functions used as tags into a game project, well that's the idea. When building the dll I use:

#ifdef BUILDING_DLL
bool gameEntry(SiSE* GE){return true;}
bool gameExit(SiSE* GE){return true;}
#endif


I was hoping that when I compiled my project with the .a and using the dll and without the BUILDING_DLL defined that I would be able to overload these 2 functions and use functions defined in the project..but alas not. The 2 functions are being called constantly causing a loop and hanging the process....can anyone think of a design/method to get around this?


You can't inherit functions - only class methods.
To do what you want to achieve, I suggest you to use the strategy pattern:


class Strategy
{
public:
// inherit those
virtual bool gameEntry(SiSE *GE) = 0;
virtual bool gameExit(SiSE *GE) = 0;
};

// in your DLL
bool gameEntry(Strategy *s, SiSE *GE)
{
if (s) return s->gameEntry(GE);
else return true;
}
bool gameExit(Strategy *s, SiSE *GE)
{
if (s) return s->gameExit(GE);
else return true;
}


HTH,

Share this post


Link to post
Share on other sites
Thanks for that Emmanuel and Anon (Ermm!!!).

I was a little confused about what Emmanuel suggested. I have heard about design patterns, I'm guessing that is what the Stategy pattern is, but haven't really read anything about them yet...perhaps I should.

I solved the problem by passing the functions to the game engine class when I instansiate the Game engine object within the project. That seems to do the trick.

Thanks for trying to help though...much appreciated.

EDIT: The anonymous post seems to have gone???? Maybe I dreamt it?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Let's clear up a minor confusion between "overload" and "override":

To "overload" a function means to provide multiple functions with the same name, but different parameters. All functions can be overloaded. For example, the function foo is overloaded here: int foo( int bar ) { ... }
int foo( char * baz ) { ... } To "override" a function means to replace it through inheritance. You can't override non-member functions. For example, the function foo is overridden here: class A
{
int foo( int bar ) { ... }
};
class B : public A
{
int foo( int bar ) { ... }
}; Note that overloaded function have different parameters, but overriden functions have identical parameters.

Share this post


Link to post
Share on other sites
That should have been...

To "overload" a function means to provide multiple functions with the same name, but different parameters. All functions can be overloaded. For example, the function foo is overloaded here:
    int foo( int bar ) { ... }
int foo( char * baz ) { ... }
To "override" a function means to replace it through inheritance. You can't override non-member functions. For example, the function foo is overridden here:
    class A
{
int foo( int bar ) { ... }
};
class B : public A
{
int foo( int bar ) { ... }
};
In summary, overloaded functions have different parameters, but overriden functions have identical parameters.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this