ReturnType LongClassName::ReallyReallyReallyLongFunctionName(
Type par_name1, // 4 space indent
Type par_name2,
Type par_name3) {
DoSomething(); // 2 space indent
...
}注意以下几点:
(1)、返回值总是和函数名在同一行;
(2)、左圆括号总是和函数名在同一行;
(3)、函数名和左圆括号间没有空格;
(4)、圆括号与参数间没有空格;
(5)、左大括号总在最后一个参数同一行的末尾处;
(6)、右大括号总是单独位于函数最后一行;
(7)、右圆括号和左大括号间总是有一个空格;
(8)、函数声明和实现处的所有形参名称必须保持一致;
(9)、所有形参应尽可能对齐;
(10)、缺省缩进为2个空格;
(11)、换行后的参数保持4个空格的缩进;
如果函数声明成const,关键字const应与最后一个参数位于同一行:=
// Everything in this function signature fits on a single line
ReturnType FunctionName(Type par) const {
...
}
// This function signature requires multiple lines, but
// the const keyword is on the line with the last parameter.
ReturnType ReallyLongFunctionName(Type par1,
Type par2) const {
...
}如果有些参数没有用到,在函数定义处将参数名注释起来:
// Always have named parameters in inteces.
class Shape {
public:
virtual void Rotate(double radians) = 0;
}
// Always have named parameters in the declaration.
class Circle : public Shape {
public:
virtual void Rotate(double radians);
}
// Comment out unused named parameters in definitions.
void Circle::Rotate(double /*radians*/) {}Warning:
// Bad - if someone wants to implement later, it's not clear what the
// variable means.
void Circle::Rotate(double) {}函数调用:尽量放在同一行,否则,将实参封装在圆括号中.
函数调用遵循如下形式:
bool retval = DoSomething(argument1, argument2, argument3);如果同一行放不下,可断为多行,后面每一行都和第一个实参对齐,左圆括号后和右圆括号前不要留空格:
bool retval = DoSomething(averyveryveryverylongargument1,
argument2, argument3);如果函数参数很多,出于可读性的考虑可以在每行只放一个参数:
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-35033-24.html
面子上都过得去
清者自清