Inadvertent Use of = Instead of ==

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:

  1. Assign & Check

    The = operator (when not overriden) normally returns the value that it assigned. This is to allow statements such as a=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 to true or false 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: true

    Note: 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.

  2. Overridden = operator

    Since 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 on a with b 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 and b would be of the correct type as defined by the overriding of the = operator for objects of a'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



Leave a reply



Submit