From 2b7fc08db367de400b3a0c0d824cf0c0dde8ab44 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 18 Apr 2020 11:13:38 +0200 Subject: [PATCH] Documentation: Add section about structs vs. classes to CodingStyle.md --- Documentation/CodingStyle.md | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/Documentation/CodingStyle.md b/Documentation/CodingStyle.md index 8c7c173ed01..4ba16709174 100644 --- a/Documentation/CodingStyle.md +++ b/Documentation/CodingStyle.md @@ -381,6 +381,55 @@ signed int c; // Doesn't omit "signed". ### Classes +[](#structs-vs-classes) For types with methods, prefer `class` over `struct`. + +* For classes, make public getters and setters, keep members private with `m_` prefix. +* For structs, let everything be public and skip the `m_` prefix. + +###### Right: + +```cpp +struct Thingy { + String name; + int frob_count { 0 }; +}; + +class Doohickey { +public: + const String& name() const { return m_name; } + int frob_count() const { return m_frob_count; } + + void jam(); + +private; + String m_name; + int m_frob_count { 0 }; +} +``` + +###### Wrong: + +```cpp +struct Thingy { +public: + String m_name; + int frob_count() const { return m_frob_count; } + +private: + int m_frob_count { 0 }; +} + +class Doohickey { +public: + const String& name() const { return this->name; } + + void jam(); + + String name; + int frob_count { 0 }; +}; +``` + [](#classes-explicit) Use a constructor to do an implicit conversion when the argument is reasonably thought of as a type conversion and the type conversion is fast. Otherwise, use the explicit keyword or a function returning the type. This only applies to single argument constructors. ###### Right: