QObject* parent instead of QObject *parent



  • How can I configure Qt Creator to use the first style instead of the second his auto-generated code? Where can I find the corresponding templates?


  • Moderators

    I am not aware of such an option.

    Feel free to file a feature request in our "bugtracker":http://bugreports.qt.nokia.com/

    Of course you can also fix the issue yourself and file a "merge request":http://qt.gitorious.org/qt-creator with the changes.



  • I read in the "QtCodingStyle":http://qt.gitorious.org/qt/pages/QtCodingStyle document (section whitespace) that the preferred style is not QObject* parent but QObject *parent.

    @the Trolls: what are the advantages of the chosen style? What are the reasons that made you choose it?

    I know it is quite subjective, but I prefer the first style (QObject* parent instead of QObject *parent).


  • Moderators

    Markus: I do not know, that coding style was in effect long before I joined the company:-)

    The reasoning I saw somewhere else was that the latter style makes it more clear that the name "parent" is a pointer since the * goes with the name of the variable.



  • I prefer the "T* name" Style because the type of name is pointer to T, so the * belongs to the type, not
    to the name. One argument for the "T *name" style is, that it is consistent to multiple variable declarations on the same line, but this is something I don't do anyway.



  • I fact you could declare more than one variable using this code:

    QObject *pParent = NULL, *pParent2 = NULL; // Two pointer

    But is différent from

    QObject *pParent = NULL, pParent; // One pointer and a none pointer object.

    So I think QObject pParent is better than QObjet pParent;

    QObject pParent : The variable is a pointer on a objet of type QObject. QObject is not a class but a pointer on a class. The variable is a pointer, so * should be next the variable.

    Don't you agreed?

    note : I use QObject* pParent"since 6 years but try to change this.


  • Moderators

    DrMaboule: QObject based objects should not get allocated on the stack, since mixing memory management based on the C++ stack with Qt's parent-child relation can get really messy:-)

    I consider the information "pointer of type A" is better transported by writing A*. I hardly ever see "A a, b" in code, so I really do not consider whether that is a bit more readable or not at all important.

    In the end the most important thing is to be consistent with the surrounding code, so I end up using whichever style is already there. In code not effected by existing coding standards I go for "A*".



  • For C language

    @MyStruct *myStruct@

    , for C++

    @MyClass* myClass@.

    This recomendation from "Bjarne Straustrup":http://www2.research.att.com/~bs/bs_faq2.html#whitespace



  • Thanks for this answer and this link.

    See you in another topic.


  • Moderators

    SABROG: Oh, you broke the thread:-)

    Who dares to contradict Stroustrup?



  • Panke, see the Qt Creator plugin gallery "here":http://developer.qt.nokia.com/wiki/Qt_Creator_Plug-in_Gallery . There is AStlyle plugin. But there is a limitation, it is only for Qt Creator 1.3.1. We'll hope it gets updated.



  • [quote author="DrMaboule" date="1279318875"]I fact you could declare more than one variable using this code:

    QObject *pParent = NULL, *pParent2 = NULL; // Two pointer

    But is différent from

    QObject *pParent = NULL, pParent; // One pointer and a none pointer object.

    This is something I don't do anyway. I would call this a code smell on its own.
    [/quote]
    [quote]
    QObject pParent : The variable is a pointer on a objet of type QObject. QObject is not a class but a pointer on a class. The variable is a pointer, so * should be next the variable.
    [/quote]

    You are right, QObject* is not a class, but a pointer to a class. But it is atype.

    [quote]
    Don't you agreed?
    [/quote]
    Obviously no, but it is a matter of taste. I'll agree to Tobias Hunger that you should be consistent
    to the code at hand and for my code it's T* name

    lyuts thank you for the info.


Log in to reply
 

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