Jump to content
  • Advertisement

Archived

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

Yanroy

Overloading using Macros and Inline Functions

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

If I want to change the parameters of a C/C++ library function by overloading it, is that a bad idea? And also, should I use a Macro or an inline function? Take the following example:
#define strcmp(a,b) !strcmp(a,b)
 
-or-
inline bool strcmp(char *a, char *b)
{
if (!strcmp(a,b))
return true;
return false;
}
 
Do you understand what I am trying to do? Right now, strcmp returns false when the two strings match... I want it to return true. --------------------

You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

Share this post


Link to post
Share on other sites
Advertisement
If you do it the way you write either of those, you''ll be in an endless loop, because it is calling itself... there is no way to distinguish the two functions.
Your best bet is a macro like

COMPARE( a, b ) !strcmp( a, b )


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites
Overloading will only work if the function arguments are different from the original function. If the only difference is in the return type, it won''t work.

In this case, however, it''ll work because the C library declares strcmp as strcmp(const char*, const char*), if I''m not mistaken.

Isn''t it easier to simply use ! in your if evaluation expression? Like if (!strcmp(a,b) { ... }?

People expect strcmp to behave this way, and by using a macro it can possibly mess up existing code.

Erik

Share this post


Link to post
Share on other sites
*pounds head against table*

Of course... I knew that ... really.... just testing you Good thing I didn''t actually try coding that... I would have spend hours debugging my program, trying to find out why it hangs at the first strcmp. I will go with a macro by a different name...

Does that remind you of some poet (shakespeare?)
A macro by any other name would still be a #define

--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

Share this post


Link to post
Share on other sites
Is is really that much harder to write

if (strcmp(a, b) != 0)
// do something

I would say to stick with that since everyone else will know what you are talking about. The things about strcmp (I belive, correct me if I''m wrong) is that the return value can tell you more about the relationship between the strings than just equality or not by returning various positive, negative or zero values.

Since we''re on this... why not just do this:

if (a == b)
// do something

where a and b are std::string Its a lot cleaner than either of the other alternatives!

Check out the GPI project today!

Share this post


Link to post
Share on other sites
You''d better not write a==b. There are some filthy things about the == operand while using char

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if you want to overload the function, use:


inline bool strcmp(const char* a, const char* b)
{ return !std::strcmp(a, b) }


however, your not saving any execution time, and the compiler problems or code readability problems caused by this far outweigh the one character ! you need to type

Share this post


Link to post
Share on other sites
CJ: Yeah, I know about the "filthy things" that happen when you use == to compare char*... since they are pointers the memory addresses will be compared for equality, not the values they point to. But it you are using std::string, then you are no longer dealing with pointers so the == operator will function properly.

Check out the GPI project today!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!