简体   繁体   中英

c++ method parameter change it self only in release mode at linux

in c++ h file:

bool getIntValue(QString name,int& dest,int default_value,bool set_default=true);

in c++ cpp file:

bool CoinConfig::getIntValue(QString name, int& dest, int default_value, bool set_default)
{
    qDebug() << "1 getIntValue set_default=" << set_default;
    if (_Platform_Config->contains(name)) {
        dest = _Platform_Config->value(name).toInt();
        qDebug() << "2 getIntValue dest" << dest;
        return true;
    }
    else if (set_default) {
        qDebug() << "3 set_default=" << set_default;
        qDebug() << "4 getIntValue dest default" << dest;
        dest = default_value;
        qDebug() << "5 getIntValue dest default" << dest;
        return false;
    }
}

//////////////////////// run bellow code :

int b;
getIntValue("timer", b, -1, false); //_Platform_Config->contains(name) return false

got the output:

1 getIntValue set_default = false  // at first set_default is false 
3 set_default= true                // change to true  ???   
4 getIntValue dest default 1000
5 getIntValue dest default -1
b 2 -1

The set_default parameter change from false to true !!! the system is linux , in debug mode it is right , but in release mode it is error.

why?

There is a missing return statement in getIntValue . Try adding return true or return false at the end of the function:

bool CoinConfig::getIntValue(QString name, int& dest, int default_value, bool set_default)
{
    qDebug() << "1 getIntValue set_default=" << set_default;
    if (_Platform_Config->contains(name)) {
        dest = _Platform_Config->value(name).toInt();
        qDebug() << "2 getIntValue dest" << dest;
        return true;
    }
    else if (set_default) {
        qDebug() << "3 set_default=" << set_default;
        qDebug() << "4 getIntValue dest default" << dest;
        dest = default_value;
        qDebug() << "5 getIntValue dest default" << dest;
        return false;
    }
    return false; // <--- this is missing in your code
}

I recommend that you enable compiler warnings, then the problem is found easily:

prog.cc: In function 'bool getIntValue(QString, int&, int, bool)':
prog.cc:27:1: warning: control reaches end of non-void function [-Wreturn-type]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM