Original Post
if (time_step=intercept_time) if (time_step==intercept_time) both compiled. But the one above have right effect for my game. What is the differnece?
Quote:
Original post by Anonymous Poster
I'm assuming you're using C, C++, Java, or a syntaxic similar language. Then the = is for assignations, and the == tests for equality. The = always evaluates to TRUE, whereas == evaluates to TRUE if and only if the left and right parts of the expression are equal.
if( x = expr() )if( expr() )x = expr();if( x )if( func() == var )if( 10 == var )if( var == func() )if( var == 10 )if( 10 = var ) // Compiler will go on strikeif( var = 10 ) // you will have a hard time finding such a bug
Quote:
Original post by janta Quote:
Original post by Anonymous Poster
I'm assuming you're using C, C++, Java, or a syntaxic similar language. Then the = is for assignations, and the == tests for equality. The = always evaluates to TRUE, whereas == evaluates to TRUE if and only if the left and right parts of the expression are equal.
You sure of that ?
if ( MyObject *foo = findObjectInDatabase() ){ //this will execute iff findObjectInDatabase returns a non-NULL value}else{ //this will execute if findObjectInDatabase returns NULL}Quote:
Original post by janta Quote:
Original post by Anonymous Poster
I'm assuming you're using C, C++, Java, or a syntaxic similar language. Then the = is for assignations, and the == tests for equality. The = always evaluates to TRUE, whereas == evaluates to TRUE if and only if the left and right parts of the expression are equal.
You sure of that ?
I think thatif( x = expr() )
evaluates the same asif( expr() )
andx = expr();if( x )
Am I wrong ? At least do I think it's like that in C++ (and probably C) I don't remember well about Java, It might always evaluate to true.
T a = b;if( b ){}if(a = b){}#include <iostream>// _index of -1 means uninitialized// when converted to bool the object will evaluate to true// if the index is initialized and false if it isn't.class array_index{ int _index;public: array_index() : _index(-1) {} array_index( int raw_index ) : _index(raw_index){} array_index& operator=(int rhs) { _index = rhs; return (*this); } operator bool()const { return (_index != -1); }};int main(){ array_index x; if( x = 0 ) { std::cout << "x = 0 is true" << std::endl; } else { std::cout << "x = 0 is not true" << std::endl; } if( 0 ) { std::cout << "0 is true" << std::endl; } else { std::cout << "0 is not true" << std::endl; } return 0;}Quote:
Original post by Anonymous Poster
Also, in Java, = does not evaluate to anything, and so trying if( foo = bar ) will get you a nice compiler error.
Quote:
Original post by CTar
Janta: What if MyObject isn't an integer type? "if ( obj )" tries to implicitly convert obj to a boolean.
[integer = signed char, char, unsigned char, signed short int, unsigned short int, signed int, unsigned int, unsigned long int or signed long int]
array_index x;x.SetIndex(0); // or if( x.IsInitialized() ){ ... }Quote:
Original post by janta
What you've just done is a tricky class wich allowed you to "trickytransparently" convert '0' into 'true'.
Quote:
Determine whether any field of an OEResidue has a non-default value.
Angle::Angle( T in ) : actual_data( in % 360 ){}Angle::operator T(){return actual_data;}Angle& Angle::operator=( const Angle& rhs){ actual_data = rhs; return (*this);}Angle x;bool b = static_cast<bool>(x = 720);if (foo = bar()) // ASSIGNMENT...Quote:
Original post by Verg
Another aside:
If you DO decide to use an assignment as a test, make sure and comment it so that future coders knew you did it on purpose.
EG:if (foo = bar()) // ASSIGNMENT...
"foo = bar()" might look like a bug to someone else.
Chad
Quote:
Original post by taby Quote:
Original post by Verg
Another aside:
If you DO decide to use an assignment as a test, make sure and comment it so that future coders knew you did it on purpose.
EG:if (foo = bar()) // ASSIGNMENT...
"foo = bar()" might look like a bug to someone else.
Chad
Or even specify completely...
if(0 != (foo = bar()))
{
...
}
if (SomeObject *pObj = FuncReturningObjPointer()){ // do stuff with pObj }SomeObject *pObj = NULL;if (pObj = FuncReturningObjPointer()){ // do stuff with pObj }Quote:
Original post by ChaosEngine Quote:
Original post by taby Quote:
Original post by Verg
Another aside:
If you DO decide to use an assignment as a test, make sure and comment it so that future coders knew you did it on purpose.
EG:if (foo = bar()) // ASSIGNMENT...
"foo = bar()" might look like a bug to someone else.
Chad
Or even specify completely...
if(0 != (foo = bar()))
{
...
}
good god, that's ugly. Please don't do that.
there's nothing wrong withif (SomeObject *pObj = FuncReturningObjPointer()){ // do stuff with pObj }
it's clean, it limits the scope of pObj, and it's obvious it's not meant to be an equality test
the same cannot be said forSomeObject *pObj = NULL;if (pObj = FuncReturningObjPointer()){ // do stuff with pObj }
there's really no need for this. If pObj is null, what use is it?
if((result = someOperation()) != GOOD) // handle errorboolean a, b;...if(a = b) // Sets a to b and returns the result as the condition of the if statementThis topic has been locked by a moderator. New replies are not allowed.
GameDev.net uses cookies to ensure you have the best experience on our platform. Learn more