NS3的代码风格(二)
(参考官方文档,且与官方文档不完全相同)
3. 文件结构
一个名为MyClass
的类应该被声明在名为my-class.h
的头文件里,而它的具体实现应该在名为my-class.cc
的源文件里。
每一个my-class.h
头文件都应以以下注释开始:
/*
* Copyright (c) YEAR COPYRIGHTHOLDER
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: MyName <myemail@example.com>
*/
头文件保护符(MY_CLASS_H)的使用。
#ifndef MY_CLASS_H
#define MY_CLASS_H
namespace n3 {
/**
* \brief short one-line description of the purpose of your class
*
* A longer description of the purpose of your class after a blank
* empty line.
*/
class MyClass
{
public:
MyClass ();
/**
* \param firstParam a short description of the purpose of this parameter
* \returns a short description of what is returned from this function.
*
* A detailed description of the purpose of the method.
*/
int DoSomething (int firstParam);
private:
void MyPrivateMethod (void);
int m_myPrivateMemberVariable;
};
} // namespace ns3
#endif /* MY_CLASS_H */
my-class.cc
文件也类似。
/*
* Copyright (c) YEAR COPYRIGHTHOLDER
*
* 3-paragraph GPL blurb
*
* Author: MyName <myemail@foo.com>
*/
#include "my-class.h"
namespace ns3 {
MyClass::MyClass ()
{}
...
} // namespace ns3
4. 注释
一般情况下,一行注释使用“//”,而多行的注释使用“/* */”,如下所示:
// A short comment
/*
1. A longer comment,
2. with multiple lines.
*/
5. 其他
- const引用语法:
void MySub (const T&); // Method 1 (推荐)
void MySub (T const&); // Method 2 (不推荐)
- 在函数名和参数列表之间最好加个空格,例如:
void MySub(const T&); // 不推荐
void MySub (const T&); // 推荐
在头文件中尽量不要使用内联函数,将所有的函数实现都尽量写在
.cc
文件中。不要使用“NULL”常量,用“0”取代之。
- 尽量不要使用C++的标准库的命名空间,如避免使用“
using namespace std;
”。