July 15, 2010

Panke Panke
Lab Rat
42 posts

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?

11 replies

July 15, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3224 posts

I am not aware of such an option.

Feel free to file a feature request in our bugtracker [bugreports.qt.nokia.com]

Of course you can also fix the issue yourself and file a merge request [qt.gitorious.org] with the changes.

July 16, 2010

Markus Liebe Markus Liebe
Lab Rat
40 posts

I read in the QtCodingStyle [qt.gitorious.org] 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).

July 16, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3224 posts

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.

July 16, 2010

Panke Panke
Lab Rat
42 posts

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.

July 17, 2010

DrMaboule DrMaboule
Lab Rat
2 posts

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.

July 17, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3224 posts

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*”.

July 17, 2010

SABROG SABROG
Lab Rat
82 posts

For C language

  1. MyStruct *myStruct

, for C++

@MyClass* myClass@.

This recomendation from Bjarne Straustrup [.research.att.com]

 Signature 

QT - Apple QuickTime
Qt - Nokia’s Qt Development Frameworks
Apple is a competitor of Nokia, so QT != Qt

July 17, 2010

DrMaboule DrMaboule
Lab Rat
2 posts

Thanks for this answer and this link.

See you in another topic.

July 17, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3224 posts

SABROG: Oh, you broke the thread:-)

Who dares to contradict Stroustrup?

July 18, 2010

lyuts lyuts
Lab Rat
169 posts

Panke, see the Qt Creator plugin gallery here [developer.qt.nokia.com] . There is AStlyle plugin. But there is a limitation, it is only for Qt Creator 1.3.1. We’ll hope it gets updated.

 Signature 

I’m a rebel in the S.D.G.

July 18, 2010

Panke Panke
Lab Rat
42 posts

DrMaboule wrote:
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.



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.

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


Don't you agreed?

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.

 
  ‹‹ Building QtMobility docs      Qt Creator for windows 64 bit ››

You must log in to post a reply. Not a member yet? Register here!