Sign in to follow this  
jordi_0071

the operators ? : ?

Recommended Posts

hey i write this code: #include <iostream> using namespace std; #define maximum(a, b) (a < b) ? b : a int main() { int max = maximum(5,3); cout << "The maximum off 5 and 3 is " << max << "." << endl; system("pause"); return 0; } but what does this ? b : a do?

Share this post


Link to post
Share on other sites
In front of the ? there is a condition. If this is true, the value between the ? and the : is given back, otherwise the value following the : is returned.

Example:


int Maximum(int a, int b)
{
return (a > b) ? a : b;
}



is the same as


int Maximum(int a, int b)
{
if (a > b)
return a;
else
return b;
}



Is it now clear to you how it works?

Share this post


Link to post
Share on other sites
Quote:
Original post by jordi_0071
but i dont know where the "?" and ":" is for?
They're used to separate the condition and two possible resulting expressions.

This would be the equivalent C code:
int maximum(int a, int b) {
if(a < b) {
return b;
} else {
return a;
}
}

Share this post


Link to post
Share on other sites
In C++, please don't use a #define for this, use an inline function (it's safer, and actually part of the language, rather than a text-substitution hack added on afterwards). If you want it to work for all types, you can use a template:


// For example, this way requires a and b to be the same type, but that type could
// be anything that is comparable with <

template <typename T>
inline T& maximum(const T& a, const T& b) {
return (a < b) ? b : a;
}


This will almost certainly result in the same generated code as the define in simple cases (it's a little different because you're giving the compiler final say in whether or not it actually gets inlined or becomes a separate function - but trust me, the compiler knows better than you on this), and will avoid weird unexpected results in more complicated cases.

However, for this function specifically, you should be aware that it is already provided as "max()" (there is also min()) in the std namespace, under header <algorithm>. Don't reinvent the wheel (but read this if you're using windows.h).

Share this post


Link to post
Share on other sites
Also, one more nifty thing you should remember: You -can- nest an ? : statement inside an ? : statement, although it's messy.

Example:



inline float clamp( float A, int B, int C )
{
return ( ( A < B ) ? ( B ) : ( ( A > B && A < C ) ? ( A ) : ( C ) ) );
};





( QUESTION: Since it is so messy, can I get someone else to maybe nitpick my code and tell me what I should do different please? )

( NOTE: I have that inline'd, since I'm using C++ ( where Macros are generally hated ) )

( NOTE2: Yes, I had to make that code( with the help of EDI, we had to experiment to figure out the best way to nest a ? : statement inside another ? : statement. ), because I was "refactoring" some of the things in the Enginuity series to make it use alternate ( usually custom ) functions that do the same things that the ones from the 3rd party librarys used by superpig. This would remove the need to link to SDL, GL/U/X ( GLU or GLX :P ), or FMOD. ( I only "refactored" the pieces that I saw nifty, and I'll probably end up rewriting the code anyways. ) Anyways, sorry for the long NOTE :P )

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