Qt World Summit: Register Today!

C++ "or"

  • Heya, I'm looking for something that say a variable is invalid it takes another one instead. In lua we had something like this : time = player.getTime() or 0. If player.getTime() was invalid, it would instead use 0. Is it possible to do this in c++ with not too much code?

  • You can use

    time = "player.getTime() is valid" ? player.getTime() : 0;

  • [quote author="Johan Solo" date="1396865569"]You can use

    time = "player.getTime() is valid" ? player.getTime() : 0;

    Doesn't seem to work, am I doing this correctly?

    @s = QLocale(QLocale::English).toString(totTaxes)? QLocale(QLocale::English).toString(totTaxes) : 0;@

    totTaxes is retrieved from a textEdit, I want s to be 0 if the player hasn't filled anything in the textEdit

  • @s = QLocale(QLocale::English).toString(totTaxes) != "" ? QLocale(QLocale::English).toString(totTaxes) : 0;@

    this should work. It compares to an empty string.

    Note: that does call the whole stuff including toString twice. In your case not a problem, but it may be with other calls.

    [edit, koahnig]

  • Moderators

    You forgot parentheses:
    s = (QLocale(QLocale::English).toString(totTaxes) != "") ? QLocale(QLocale::English).toString(totTaxes) : 0;
    and it will still not compile becase toString and 0 are different types.

    It's also better to use isEmpty() instead of comparison, because "" will have to be converted to Qstring and then operator= will be called which is heavier than simple emptiness check (probably just reading a counter):
    auto tax = QLocale(QLocale::English).toString(totTaxes);
    s = tax.isEmpty() ? QStringLiteral("0") : tax;

  • Thanks Chris.
    I am not sure, if really need to have the parentheses. But you are certainly correct with the type :(

  • I think parentheses are optional with the "?" operator, but c++ is still strongly typed that is why you can't have a string or 0, that is also the reason the "or" operator (||) cannot be used in this case, if you are dealing with values that can be compared to 0 it will work of course, e.g.
    int a = 0;
    int x = a || 42; // of a is 0 then 42 should be used
    also with pointers and stuff, but in general you can only use the "or" with scripting languages (like Lua, PHP or JavaScript), because it implicitly converts many values to "false", that is why you can do that in Lua, but not in c++.

    Chris answer should correct of course, but I am wondering in what case could the string be empty if you convert a number to a QString?? Unless "totTaxes" is not a number, so to the OP can you get as some examples for your "totTaxes" variable and what type it is? :D

  • Moderators

    I apologize for the parenthesis comment. It was early, I haven't had my coffee yet and I was thinking of the "common ternary operator pitfall":
    if(a == func() ? true : false) //this is what was written
    if(a == (func() ? true : false)) //this is what was meant
    if((a == func()) ? true : false) //this is what we got
    This of course does not apply in this case as what koahnig wrote is what he meant :)

    Ints actually do convert automatically to bools in c++ too.
    This example
    @int x = a || 42;@
    actually works like this (though it might not be obvious):
    @int x = (int)((bool)(a) || (bool)(42)) //because || operates on bools
    //so it's actually:
    int x = (int)((bool)(a) || true) //value of a doesn't matter here
    //which is
    int x = (int)(true);
    //which is
    int x = 1;@
    This is almost always a typo and what is meant was:
    int x = a | 42; //binary "or"
    //which is 42 for a==0, 43 for a==1 etc.

    Getting back to the topic: OP said "totTaxes" is taken from textEdit, which I assume is QTextEdit so it won't actually work with any of our code because it's a QString :)

    Assuming s is a QString too it should be something like this then:
    s = totTaxes.isEmpty() ? QStringLiteral("0") : totTaxes;
    without any conversions (except maybe some code to validate the string for some specific format or numerical representation).

Log in to reply