macro
Hi
how to write a macro which compares the two number and results the max number.
vishal
# 1 Re: macro
If you're using C++, the short answer is: don't.
Macros have a habit of biting you badly when you least expect it. They're hard to debug and often have unexpected effects. An inline function is almost always better than a macro.
As for your question: have you explored the possibility of looking up "max" in the standard library?
# 2 Re: macro
no i haven't but i like to know how the max function in library works
actually i am a newbie in c/c++ so i like to know how macro can be used
# 3 Re: macro
Search through your compiler's #include directories until you find
max. It's in one of them... Anyway, the C++ version of max is
typically a templatized function that looks something like this:
template <typename T>
T max(const T& lhs, const T& rhs)
{
return (lhs > rhs ? lhs : rhs);
}
Er ... that's just what I'm assuming anyway. I didn't look it up and
my Josuttis book is at work :)
--Paul
# 4 Re: macro
And I'm an oldbie, and my advice is to forget about macros when using C++.
Here's a hint: have you come across the ternary operator?
//ternary operator
<value> = <condition> ? <true-part> : <false-part>;
# 5 Re: macro
Paul: you just got in before me. I was trying to lead the OP along the lines of looking these things up. Without trying to be offensive to anyone, max() is such a fundamentally trivial function that I feel the OP is better served by us leading him to find the answer rather than just giving it straight. Others may disagree, but that's my take on it.
# 6 Re: macro
i have worked on ternary operator
and thanks for ur advice abt macro i will remember it
please look at my another question title as "mystery" and see if u can help me
# 7 Re: macro
Originally posted by Graham
Paul: you just got in before me. I was trying to lead the OP along the lines of looking these things up. Without trying to be offensive to anyone, max() is such a fundamentally trivial function that I feel the OP is better served by us leading him to find the answer rather than just giving it straight. Others may disagree, but that's my take on it.
Yeah, I kind of implied that he should look it up too. I was just
doing that max freehand so I didn't really consider it um ...
official or anything. For that, I'd expect him to look it up. I don't
think I've ever seen max(), but that's what I'd expect it to look
like...
Perhaps I should have said in my post that that was MY version
of max() so he'd have incentive to go look it up :)
--Paul
# 8 Re: macro
I suspect that any sensible definition of max() would look exactly like that. I suppose someone could come up with a 40-line function to achieve the same effect... :D