Archive

Archive for January 31st, 2010

OMNet++ 中的 NED 语言学习(4)

January 31st, 2010 leeing No comments

本节主要是关于信道(channel)的相关知识。

信道封装了与连接关联的参数和行为,信道与简单模块类似,在它们背后就是C++类,在默认情况下,类名与NED的类型名相同,除非有一个@class属性(@namespace也需要注意),例如,下面的信道类型需要一个名为CustomChannel 的C++ 类。

channel CustomChannel // needs a CustomChannel C++ class
{
}

跟简单模块不同的是,一般情况下不需要自己来定义信道类型,只需要从系统中已经预定义好的信道类型进行特殊化即可,内建的类型有: ned.IdealChannel, ned.DelayChannel and ned.Datarate-Channel。 (“ned” 是包名,和Java类似的是可以用import ned.* 来导入所有这三种类型,这样就不用写前缀ned)。

IdealChannel

  • 没有参数,可以让所有数据没有任何延迟地传送。

DelayChannel

  • 有两个参数。
  • delay是一个double类型的参数,代表传播时延,可以用s,ms,us来指定。
  • Disabled是一个布尔值,默认为false,当设为true时,信道对象会丢弃所有的信息。

DatarateChannel

  • 与DelayChannel相比,它有更多的参数。
  • datarate是一个double型的参数,代表信道的数据率,可以用bps,Kbps,Mbps,Gbps来指定,默认值是0,代表无限带宽。
  • ber和per分别代表比特错误率和包错误率,并且允许基本的错误建模。它们是随机生成的范围在0到1之间的数值。通过在包对象中设置一个错误的标记(error flag),接收的模块进行检查并丢弃标记为冲突的数据包。它们的默认值都是0。

注意: There is no channel parameter that would decide whether the channel deliversthe message object to the destination module at the end or at the start of the reception; that is decided by the C++ code of the target simple module. See the setDeliverOn-ReceptionStart() method of cGate.

Read more…

Categories: NED, OMNeT++ Tags:

C++ 中 struct 和 class 的联系和区别

January 31st, 2010 leeing 1 comment

struct vs class

在 C++ 中class 和 struct  只有两点主要区别:

  • 默认继承权限。默认情况下,class的继承是以private来继承而struct则是按照public进行继承。
  • 成员的默认访问权限。class的成员默认是private权限,struct默认是public权限。

而其它的特性,struct和class基本上,甚至严格来说是一样的:

//一个不常见的示例,将 struct 直接改为class也能编译通过。
//编译环境为 GCC 4.4.1
#include <iostream>
#include <string>
using namespace std;

struct bar
{
    private: // 访问权限修饰符
        int y;
    public:
        bar(){}; //无参构造函数
        bar(int a){ y = a;}//带参数的构造函数
        ~bar(); //虚构函数
        void say();
        virtual void func1() = 0; //纯虚函数
};

struct  foo: protected bar // 继承
{
    private:
          int x;
    public:
         foo(){};
         void say(string msg) {cout<<msg<<endl;}
         virtual int func2();//虚函数

};

int main() {
    return 0;
}

可以看到:

  • 都可以有成员函数:struct可以包含和class中一样的构造函数,析构函数,重载的运算符,友元类,友元结构,友元函数,虚函数,纯虚函数,静态函数;
  • 尽管默认访问权限不同,但都可以拥有public/private/protected修饰符;
  • 都可以进行复杂的继承和多重继承,一个struct可以继承自一个或多个class,反之亦可。
  • 注意这里与C语言并不相同,C 语言中的 struct 从本质上来说只是一个包装数据的语法机制。

Read more…

Categories: C++ Tags: