Advertisement

Overloading using Macros and Inline Functions

Started by May 04, 2000 07:19 AM
6 comments, last by Yanroy 24 years, 7 months ago
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.

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

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

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**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Advertisement
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
*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.

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

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

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!
You''d better not write a==b. There are some filthy things about the == operand while using char
Advertisement
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
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!

This topic is closed to new replies.

Advertisement