C++ class member declaration



  • I found this member declaration on a sample code, I don't understand the ";" on the parameters passed to the function.
    bool DbManager::addPerson(const QString& name)
    {
    bool success = false;
    // you should check if args are ok first...
    QSqlQuery query;
    query.prepare("INSERT INTO people (name) VALUES (:name)");
    query.bindValue(":name", name);
    if(query.exec())
    {
    success = true;
    }
    else
    {
    qDebug() << "addPerson error: "
    << query.lastError();
    }

    return success;
    }


  • Moderators

    @Dan3460
    Which ";" do you mean?


  • Lifetime Qt Champion

    Hi,

    If you mean the : before name it's to give your binding a name. It's explained here.

    You could be using another name for that variable if you wanted. Using the same just makes it clearer that you are going to put a value in the name column using a binding that is also called name.



  • Sorry, I should have looked at my copy before I posted.
    The code that i got said something like:
    bool DbManager::addPerson(const QString& instring; name)
    that ";" before name tripped me off. Now I cannot find the original post, or it was changed.
    Does that ";" makes any sense on a function implementation?



  • @Dan3460 No, that declaration is invalid in C++


  • Moderators

    It's probably a typo of some sort, but I'm feeling playful today, so I'll say it's not invalid c++. See below ;)

    #define instring )
    #define foo()
    #define name foo(
    
    bool DbManager::addPerson(const QString& instring; name)
    

    Perfectly valid c++ :) Note that MSVC will complain because its macro expansion is fundamentally broken.



  • Thanks Sirs, that is what I thought.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.