...
Code Block | ||
---|---|---|
| ||
// These are static variables for a class static int _you_are_reading_this{0}; static bool _nice_thanks_for_reading = false; static double _this_is_super_fun = 0.0f; |
File-Scope Global Variables
Should always be static and named like static class member variables:
Code Block |
---|
static int _i_am_a_static_integer = 0; |
Constants
Constants (const
and constexpr
) should be named using camel case, with the first character being a k
:
...
Classes
Classes use PascalCase. Public member variables and methods will be declared first, followed by protected and then private:
Code Block | ||
---|---|---|
| ||
class ExampleClass {
public:
...
protected:
...
private:
...
}; |
Member Variables
Non-static member variables should use snake case.
Constants should use camel case with a k
as the first character.
Static variables should use snake case, but have a _
as the first character.
Code Block | ||
---|---|---|
| ||
class ExampleClass {
public:
private:
int hello_there;
const double kGeneralKenobi;
static int _its_over_anakin;
};
// Remember static variables must be redeclared outside of the class declaration
int ExampleClass::_its_over_anakin {0}; |
Constructors
We prefer to use initializer lists when initializing member variables, however there are cases where we should stick to initializing member variables within the constructor.
Code Block |
---|
class ExampleClass {
public:
ExampleClass();
ExampleClass(int num_apple_pies);
private:
int apple_pies, banana_pies;
double slices_left;
}
ExampleClass::ExampleClass() : apple_pie {0}, banana_pies {0}, slices_left {0.0f} {}
// You can stack your initializing list. Just make sure they start in the same column
ExampleClass::ExampleClass(int num_apple_pies) : apple_pies {num_apple_pies},
banana_pies {0} {
slices_left = 6 * num_apple_pies; // If an operation is required, preferred if initialization is within constructor
} |
Initializing Classes
When statically declaring a struct, you should initialize it immediately. If a default constructor exists, call that, else call another constructor.
Code Block |
---|
class ExampleClass {
public:
ExampleClass();
ExampleClass(int num_apple_pies);
private:
int apple_pies, banana_pies;
double slices_left;
}
int main() {
ExampleClass default_class {}; // Preferred over `ExampleClass class_with_apples()`
ExampleClass class_with_apples {1}; // Preferred over `ExampleClass class_with_apples(1)`
} |
If declaring a class pointer, initialize to nullptr
if no other options exist:
Code Block |
---|
class ExampleClass {
...
};
int main() {
ExampleClass * example_pointer = nullptr; // Good!
ExampleClass statically_declared_class {};
ExampleClass * other_pointer = &statically_declared_class; // Also good!
ExampleClass * one_more_pointer = functionThatReturnsAClassPointer(); // Good too!
} |
Structs
Structs should only be used to store Plain Old Data (POD). That means no methods should ever be declared in a struct. If you need method, use a class :))
Structs will use PascalCase for their names. We will use the following syntax:
Code Block |
---|
struct StructName {
...
}; |
Initializing Structs
When statically declaring a struct variable, always initialize upon declaration:
Code Block | ||
---|---|---|
| ||
StructName struct_variable {}; // Value initialization |
When declaring a struct variable pointer, initialize it to nullptr
if no other options exist:
Code Block |
---|
StructName * struct_variable = nullptr;
StructName * struct_variable_two = functionThatReturnsAStructNamePointer(); // Valid too! |
Enums
We use PascalCase for enunms, and the constants will use all caps with underscores separating words. Additionally, the first constant should have a = 0
appended to it.
...