mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-08-16 08:50:26 +03:00
Documentation: Add section about structs vs. classes to CodingStyle.md
This commit is contained in:
parent
30de1f610d
commit
2b7fc08db3
Notes:
sideshowbarker
2024-07-19 07:31:23 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/2b7fc08db36
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user