Skip to content
update CodingStyleCppQt.md authored by frostasm's avatar frostasm
......@@ -117,6 +117,8 @@ private:
void *gSomePointer = nullptr; // gCamelCase - global non constant variable
// global non constant variables - not recommended for use
const int SomeGlobalConstant = 6; // CamelCase
int main(int argc, char *argv[])
......@@ -143,7 +145,8 @@ int main(int argc, char *argv[])
class QTimer;
// using namespace std; WRONG: Not allowed "using namespace XXX;" in header files
using namespace std; - WRONG
// WRONG: Not allowed "using namespace XXX;" in header files
namespace ProjectNamespace { // CamelCase
......@@ -163,16 +166,23 @@ class SomeProjectClass : public QObject // CamelCase
Q_PROPERTY(bool mouseTracking READ hasMouseTracking WRITE setMouseTracking)
public:
explicit SomeProjectClass(QObject *parent = nullptr);
explicit SomeProjectClass(int i, QObject *parent = nullptr);
explicit SomeProjectClass(QObject *parent = nullptr); // explicit - necessarily, if not assumed otherwise
explicit SomeProjectClass(int i, QObject *parent = nullptr); // explicit - necessarily, if not assumed otherwise
// in classes it is forbidden to use not static public members only allowed for struct
// We suggest to use initialization with {} brackets for Primitive Built-in Types. [1]
bool publicField{false}; // camelCase
// Use nullptr instead of 0 and NULL [2]
QObject *someQObject{nullptr}; // camelCase
// in classes it is forbidden to use not static public members
// only allowed for struct
int publicField; // camelCase
static const int PublicClassConstant{17}; // CamelCase
static const int PublicClassConstant; // CamelCase
// Use alias declarations instead of typedefs [3]
using MyList = QList<MyType>; // instead of - typedef QList<MyType> MyList;
// for compilers which support c++11 standard
// Prefer scoped enums to unscoped enums. [4]
enum class ClassEnum[: std::type] { // CamelCase. [: std::type] - optional
Value = 0, // CamelCase, not CEValue or CE_Value
AnotherValue // CamelCase, not CEAnotherValue or CE_AnotherValue
......@@ -185,63 +195,60 @@ public:
// in the General case, it is not recommended to leave the implementation in the header
int smallFunc() { return 7; } // in header use one line function if possible
// Usually order of access labels like this
signals:
// in the General case, the order of the access sections the following
public: // functions block
public slots:
signals:
protected: // functions block
protected slots:
protected: // members-data block
static fields;
fields;
protected:
// FUNCTIONS
...
// FIELDS
...
private slots:
private:
// FUNCTIONS
private: // functions block
void somePrivateFunc(); // camelCase - not m_camelCase
// static functions
void static somePrivateStaticFunc(); // camelCase
// FIELDS
int m_privateField = -1; // m_camelCase
private slots:
QTimer *m_syncTimer = nullptr; // use forward declaration instead #include
private: // members-data block
// static fields
static QString s_classPrivateStaticField; // s_camelCase
static const int ClassPrivateConstantStaticField = 6; // CamelCase
int m_privateField{-1}; // m_camelCase
// raw pointers can only be used in case when they inherits from QObject and when they have a parents.
// For all other cases, it is necessary to use various types of guarded/scoped/shared pointers
QTimer *m_syncTimer{nullptr}; // use forward declaration instead #include
// Тут є думка можливо більш виразніша наступна структура
// QPointer- when the object is not our child and we need to track the time of its existence
QPointer<SomeQObject> m_trackObject; // use forward declaration instead #include
// functions block
public:
public slots:
// чому сигнали після public?
// тому що в паблік буває описуються типи даних які юзаються в public slots і signals
signals:
// QScopedPointer - to ensure that the pointer is destroyed along with our instance of object
//
QScopedPointer<QImage> m_tempImage; // use forward declaration instead #include
/*
QScopedPointer Forward Declared Pointers
protected:
protected slots:
https://doc.qt.io/qt-5/qscopedpointer.html#forward-declared-pointers
Classes that are forward declared can be used within QScopedPointer, as long as the destructor
of the forward declared class is available whenever a QScopedPointer needs to clean up.
Concretely, this means that all classes containing a QScopedPointer that points to a forward declared class
must have non-inline constructors, destructors and assignment operators:
*/
private:
private slots:
// QSharedObject - when we need to share object between multiple objects
// and to ensure that the pointer is destroyed along with last instance of object that uses it
QSharedObject<QMap<QString, QString>> m_nameChace; // use forward declaration instead #include
// fields block
protected:
static fields;
fields;
private:
static fields;
fields;
// static fields
static QString s_classPrivateStaticField; // s_camelCase
static const int ClassPrivateConstantStaticField{6}; // CamelCase
};
}
......@@ -254,11 +261,13 @@ private:
```cpp
#include "SomeProjectClass.h"
#include "SomeOtherProjectClass.h"
#include <SomeGlobalClass.h>
#include <QTimer>
#include <QImage>
#include <iostream>
......@@ -266,7 +275,8 @@ namespace ProjectNamespace { // not use new line before bracket in namespace def
QString SomeProjectClass::s_classPrivateStaticField = QStringLiteral("const char * string"); // s_camelCase
SomeProjectClass::SomeProjectClass(QObject *parent) : QObject(parent) // object member initialization in same line with funcion declaration if possible
// use c++11 feature declaration constructors
SomeProjectClass::SomeProjectClass(QObject *parent) : SomeProjectClass(0, parent) // object member initialization in same line with funcion declaration if possible
{
}
......@@ -300,3 +310,10 @@ void SomeProjectClass::somePublicFunc(int firstParam, QEvent *ev)
} // in source file maximum allowed 2000 lines
```
### Literature
[1] - Scott Meyers. Effective Modern C++. 42 SPECIFIC WAYS TO IMPROVE YOUR USE OF C++11 AND C++14. CHAPTER 3. Moving to Modern C++. Item 7: Distinguish between () and {} when creating objects.
[2] - Scott Meyers. Effective Modern C++. 42 SPECIFIC WAYS TO IMPROVE YOUR USE OF C++11 AND C++14. CHAPTER 3. Moving to Modern C++. Item 8: Prefer nullptr to 0 and NULL.
[3] - Scott Meyers. Effective Modern C++. 42 SPECIFIC WAYS TO IMPROVE YOUR USE OF C++11 AND C++14. CHAPTER 3. Moving to Modern C++. Item 9: Prefer alias declarations to typedefs.
[4] - Scott Meyers. Effective Modern C++. 42 SPECIFIC WAYS TO IMPROVE YOUR USE OF C++11 AND C++14. CHAPTER 3. Moving to Modern C++. Item 10: Prefer scoped enums to unscoped enums.