[WIP]C++ Coding Style
- 초기화
int x{10};
- Type cast
- built-in types
int x = (int) 10;
- UDTs
Month month = Month(m);
- Const
- compile-time
#define JAN 1 #define FEB 2 ... enum { JAN = 1, FEB, ... }
- runtime
const int kJan = 1; const int kFeb = 2; ...
- Naming Rules
- class
class MyClass { ... }
- function
int MyFunction() { ... }
- local variable
int my_var;
- global variable
All global variables should have a comment describing what they are, what they are used for, and (if unclear) why it needs to be global. For example:
// The total number of test cases that we run through in this regression test. const int kNumTestCases = 6;
- member variable
double my_var_;
- inline function
- function bodies in the class declaration
- 한 줄로 표기
int getNum() { return num; }
- 멤버 변수 설정 및 반환
int Var(void) const;
void SetVar(int v);
type var_name_;
type VarName(param declaration, if any) const;
void SetVarName(type var_name);
- 파라미터에 주석을 쓰는 경우
Unused parameters that might not be obvious should comment out the variable name in the function definition:
class Shape {
public:
virtual void Rotate(double radians) = 0;
};
class Circle : public Shape {
public:
void Rotate(double radians) override;
};
void Circle::Rotate(double /* radians */) {}
- Indentation
// In the .h file
namespace mynamespace {
// All declarations are within the namespace scope.
// Notice the lack of indentation.
class MyClass {
public:
...
void Foo();
};
} // namespace mynamespace
// In the .cc file
namespace mynamespace {
// Definition of functions is within scope of the namespace.
void MyClass::Foo() {
...
}
} // namespace mynamespace
- References:
- Google c++ style guide. (n.d.). Retrieved March 15, 2021, from https://google.github.io/styleguide/cppguide.html.
Leave a comment