NS3的代码风格(二)

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;”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值