Syntax question : How to differenciate several meanings of the symbol & (ampersand) ?



  • I'm learning how to understand Qt documentation.
    And i understand the first parameter of the function addTab(), but don't understand the second one :
    Here is the function:

    • addTab ( QWidget * page, const QString & label )

    I understand the first one :

    If you want to use the addTab() function, you have to put as first parameter a pointer which is type of QWidget.(in the syntax prototype above, the pointer's name is page but you can give name it whatever you want).

    I don't understand the second one:

    I've read that if the ampersand is before a letter, then it becomes a shortcut key combination.
    But i also know from my lessons that "&" means "the adress of...", it is used to retrieve the path to a variable.
    So i'm quite confused, how to differenciate them ?
    How to know in which case which specific ampersand is used ?

    Thks in advance for any enligthenment or help :)



  • Outside of the quotation marks, the meaning of ampersand is -rather unambiguous and clear - it is- either an address of (a reference to) variable specified after that character or a logical (or bit-wise) AND operation symbol (see "JKSH post below":http://qt-project.org/forums/viewthread/28155/#126796).
    In the case of addTab function, which has two parameters, the second parameter is supposed to be a const reference to a QString object - label-to-be for the newly created tab.

    Edit: caught me on that. :-)



  • [quote author="Ground" date="1369512554"]I've read that if the ampersand is before a letter, then it becomes a shortcut key combination.
    But i also know from my lessons that "&" means "the adress of...", it is used to retrieve the path to a variable.
    So i'm quite confused, how to differenciate them ?[/quote]

    You are comparing apples and bananas here!

    In C++ syntax, the ampersand does (at least) two things:

    • If you put it in front of a variable containing an object, it will give you the address of that object, i.e. it gives you a pointer to that object.
    • If it appears in front of a parameter of a function, it means that a reference to an object will be passed to that function. A reference is pretty much the same as a pointer, except that it cannot be NULL.

    Consequently, addTab() takes a pointer to a QWidget and a const reference to a QString.

    --

    As for using an ampersand character to indicate the shortcut of a menu entry: That's only when the ampersand appears inside a string. And only if that string is used as the text that will appear inside a menu (or a similar GUI control). And even that only if the specific GUI controls supports shortcuts being defined that way...

    (So this has absolutely nothing to do with C++ syntax!)



  • Very nice and clear explanations, thks for all responses :) The fog in my head is now dissipated.


  • Moderators

    Just to make things fun, here are two more uses for ampersands in C++ :D

    Logical "AND":
    @
    bool a = true;
    bool b = true;
    bool c = false;

    bool d = a && b; // d == true
    bool e = a && c; // e == false
    @

    Bitwise "AND":
    @
    int p = 0xA; // Binary form: 1010
    int q = 0x3; // Binary form: 0011

    int r = p & q; // r == 0x2, binary form: 0010
    @


    To wrap things up: Addresses, references, logical AND and bitwise AND are part of the C++ language itself. Shortcuts are not part of C++, but some parts of some GUI frameworks interpret string-ampersands in a special way.



  • I think it's worth mentioning why const QString & label not QString & label or just QString label.

    @addTab ( QWidget * page, QString label )@

    This would involve copy operation during call. Actually QObjects are copied o demand - when object changes but QString does not inherit from QObject.

    @QString var
    addTab ( widget, var )
    //var inside addTab is copy of var@

    To avoid coping operation reference was introduced.

    @addTab ( QWidget * page, QString & label )@

    Reference acts more or less as pointers so it allows modification of parameters.

    @//wrong you cannot modify constant inside addTab
    addTab ( widget , "some text")

    //valid call
    QString var
    addTab ( widget, var )
    // var inside addTab is the same as var@

    Finally to avoid this limitation const was added to inform compiler that the parameter will not be modified

    @addTab ( QWidget * page, const QString & label )@

    It allows calling addTab with constant (or "inline") parameters.

    @//valid call
    addTab ( widget, "some tex" )@

    PS: passing string parameters works because QString has constructor that takes const char *

    Actual call looks like this

    @addTab ( widget, QString("some tex") )@



  • Great bonus knowlegdes :) thks ! It is no longer possible for me to confund any ampersand i will meet in my future :)


  • Moderators

    [quote author="8majkel8" date="1369599899"]This would involve copy operation during call. Actually QObjects are copied o demand - when object changes but QString does not inherit from QObject.[/quote]

    Sorry mate, but that is wrong... QObjects are not copyable at all (no copy constructor).

    QString and various other Qt classes (QMap, QList, QByteArray, etc.) are implicitly shared. This has nothing to do with inheritance from QObject! You can read more about it in "the docs":http://qt-project.org/doc/qt-4.8/implicit-sharing.html.


Log in to reply
 

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