Inadvertent use of = instead of ==
Most of the time, compilers try very hard to remain backward compatible.
Changing their behavior in this matter to throw errors will break existing legitimate code, and even starting to throw warnings about it will cause problems with automatic systems that keep track of code by automatically compiling it and checking for errors and warnings.
This is an evil we're pretty much stuck with at the moment, but there are ways to circumvent and reduce the dangers of it.
Example:
void *ptr = calloc(1, sizeof(array));
if (NULL = ptr) {
// Some error
}
This causes a compilation error.
3D Engine Comparison
You can find a lot of informations on lot of engines on this database.
CrystalSpace is a full engine so it's a monolithic bloc that you have to customize for your needs.
Irrlicht too but it's made do do things easy. The counter effect is that it's hard to do specific things.
Now, i think Ogre might be the most general purpose hardware accelerated 3D rendering engine around here. Maybe Horde3D is better suited for specific high quality rendering but nothing that cannot be done with Ogre too.
How to find inadvertent object pointer comparisons?
I'm pretty sure there is no such warning, as comparing pointers for equality is pretty common and not all that commonly done in error. Your best bet will unfortunately be to go through any place DataKeys might be used this way and search for ==. Not very fun, I know.
Variable assignment in an if condition
if (Derived* derived = dynamic_cast<Derived*>(base)) {
// do stuff with `derived`
}
Though this is oft cited as an anti-pattern ("use virtual dispatch!"), sometimes the Derived
type has functionality that the Base
simply does not (and, consequently, distinct functions), and this is a good way to switch on that semantic difference.
Why would you use an assignment in a condition?
It's more useful for loops than if statements.
while(var = GetNext())
{
...do something with 'var'
}
Which would otherwise have to be written
var = GetNext();
while(var)
{
...do something
var = GetNext();
}
In which case is if(a=b) a good idea?
Two possible reasons:
Assign & Check
The
=
operator (when not overriden) normally returns the value that it assigned. This is to allow statements such asa=b=c=3
. In the context of your question, it also allows you to do something like this:bool global;//a global variable
//a function
int foo(bool x){
//assign the value of x to global
//if x is equal to true, return 4
if (global=x)
return 4;
//otherwise return 3
return 3;
}...which is equivalent to but shorter than:
bool global;//a global variable
//a function
int foo(bool x){
//assign the value of x to global
global=x;
//if x is equal to true, return 4
if (global==true)
return 4;
//otherwise return 3
return 3;
}Also, it should be noted (as stated by Billy ONeal in a comment below) that this can also work when the left-hand argument of the
=
operator is actually a class with a conversion operator specified for a type which can be coerced (implicitly converted) to a bool. In other words,(a=b)
will evaulate totrue
orfalse
if a is of a type which can be coerced to a boolean value.So the following is a similar situation to the above, except the left-hand argument to
=
is an object and not a bool:#include <iostream>
using namespace std;
class Foo {
public:
operator bool (){ return true; }
Foo(){}
};
int main(){
Foo a;
Foo b;
if (a=b)
cout<<"true";
else
cout<<"false";
}
//output: trueNote: At the time of this writing, the code formatting above is bugged. My code (check the source) actually features proper indenting, shift operators and line spacing. The
<
's are supposed to be <'s, and there aren't supposed to be enourmous gaps between each line.Overridden
=
operatorSince C++ allows the overriding of operators, sometimes = will be overriden to do something other than what it does with primitive types. In these cases, the performing the
=
operation on an object could return a boolean (if that's how the=
operator was overridden for that object type).So the following code would perform the
=
operation ona
withb
as an argument. Then it would conditionally execute some code depending on the return value of that operation:if (a=b){
//execute some code
}Here,
a
would have to be an object andb
would be of the correct type as defined by the overriding of the=
operator for objects ofa
's type. To learn more about operator overriding, see this wikipedia article which includes C++ examples: Wikipedia article on operator overriding
Checking for null, which is better? null == or ==null
it's an old habit to prevent you from typing if (o = null)
. if (null = o) is a syntax error. kind of pointless in C#, because null values aren't ever coerced into booleans.
match and search in struct and mark true or false C
content [i] .mark = true
will be
content [i] .mark == true
To avoid this kind of problem you can use the comparison like this
true == content [i] .mark
In this case even if you forget the ==
and use =
the compiler will complain.
And yes even better is to use simple and easy to read
if( content [i] .mark )
Also there is no keyword called Else
in C. It will be else
.
Remove the extra }
in the code where you have written }}
. The scoping changes for that and it is wrong.
Also why use return flag = false
it's useless here. Just do return false
or return true
. Because flag
's changed value won't be used anywhere.
How do I make my code say You can't divide to 0
when you do y = temp
you are giving y the value of temp, in this case 0. In c that translates into a false
logical value
What you want to do is
if (y == temp)
the ==
operator tests the equality between the 2 variables
Related Topics
Is There a Portable Equivalent to Debugbreak()/_Debugbreak
Does Casting to an Int After Std::Floor Guarantee the Right Result
Using Export Keyword with Templates
How to Make 'New[]' Default-Initialize the Array of Primitive Types
Qt/C++ - Accessing Mainwindow UI from a Different Class
Reason Why Not to Have a Delete MACro for C++
Is There a 128 Bit Integer in C++
What Is the Proper Way of Doing Event Handling in C++
When Is a Type in C++11 Allowed to Be Memcpyed
Why Do Un-Named C++ Objects Destruct Before the Scope Block Ends
Why Is It Not Good to Use Recursive Inheritance for Std::Tuple Implementations
Complete C++ I18N Gettext() "Hello World" Example
In C++, How to Forward Declare a Class as Inheriting from Another Class
Conditional Operator Used in Cout Statement
How Bad Is Redefining/Shadowing a Local Variable
Will an 'Empty' Constructor or Destructor Do the Same Thing as the Generated One
Unoptimized Clang++ Code Generates Unneeded "Movl $0, -4(%Rbp)" in a Trivial Main()
Difference in Behavior While Using Dynamic_Cast with Reference and Pointers