demp_template.h
// Copyright 2008 Google Inc.
// License(BSD/GPL/...)
// Author: voidccc
// This is...
#ifndef PROJECT_DEMO_TEMPLATE_H_ //<project>_<path>_<file>_
#define PROJECT_DEMO_TEMPLATE_H_
#include <vector> //头文件包含顺序从小到大,优先包含自定义头文件
class Channel; //前置声明
namespace mynamespace {
class EventLoop : public CallbackInterface {
public:
typedef vector<int> IntVector; //typedef
enum UrlTableErrors { //枚举
ERROR_OUT_OF_MEMORY = 0,
ERROR_MALFORMED_INPUT = 1
}
const const_nums; //常量
explicit EventLoop(int xx); //构造函数
void AddValue(const std::string& input, Channel* output); //普通函数
int num_entries() const { return num_entries_; } //存取函数
void set_num_entries(int num_entries) { num_entries_ = num_entries; }
private:
DISALLOW_COPY_AND_ASSIGN(EventLoop); //不需要copy时在private里使用禁止copy宏
const int kDaysInAWeek = 7; //常量用k开头,后跟大写开头单词
int num_entries_; //类成员变量下划线结尾
int num_conplated_connection_;
Channel* channel_; //前置声明过了
};
} //namespace mynamespace
#endif //PROJECT_DEMO_TEMPLATE_H_
demp_template.cpp
// Copyright 2008 Google Inc.
// License(BSD/GPL/...)
// Author: voidccc
// This is a demo template for C++
// 尽量不使用宏,不适用异常,禁止使用RTTI
// 使用printf之类的代替流
// 除位域不使用无符号数字
// 除特定环境,不使用操作符重载
// 使用4种cast运算符类型转换
// 禁止使用class类型全局变量,若使用应为单列模式
// sizeof(var) 代替 sizeof(type)
// 使用scoped_ptr, share_ptr. 禁止使用auto_ptr
//头文件包含次序:
#include "demo_template.h" //1. 本类的声明
#include <sys/types.h> //2. C系统文件
#include <vector> //3. C++系统文件
#include "base/basictypes.h" //4. 其他库头文件
#include "foo/public/bar.h" //5. 本项目内头文件(避免使用unix文件路径和".", "..")
using std::string //可以在整个[C文件]和[h文件的方法]内使用using,禁止使用using namespace xx
namespace mynamespace {
EventLoop::EventLoop() : _var(xx) {} //单行初始化
EventLoop::EventLoop()
: _num_entries(10),
_num_complated_connections_(false) { //多行初始化
//...
}
ReturnType ClassName::ReallyLongFunctionName(const Type& par_name1,
tyoe* par_name2) {
bool retval = DoSomething(averyveryveryverylongargument1,
argument2, argument3);
if (condition) {
for (int i = 0; i < kSomeNumber; ++i) {
if (this_one_thing > this_other_thing &&
a_third_thing == a_fourth_thing) {
// TODO(QuentinYANG@hotmail.com): xxx
}
}
} else {
nt j = g();
}
switch (var) {
case 0: {
//...
break;
}
default: {
assert(false);
}
}
return x;
}
} //namespace mynamespace