NS_OBJECT_ENSURE_REGISTERED

定义在文件core/model/object-base.h中。代码中是这么描述这个函数的:

Register an Object subclass with the TypeId system.

从描述上来看,应该是通知TypeId系统这个新的类是Object类的子类。

/**
 * \ingroup object
 * \brief Register an Object subclass with the TypeId system.
 *
 * This macro should be invoked once for every class which
 * defines a new GetTypeId method.
 *
 * If the class is in a namespace, then the macro call should also be
 * in the namespace.
 */
#define NS_OBJECT_ENSURE_REGISTERED(type)               
  static struct Object ## type ## RegistrationClass     
  {                                                     
    Object ## type ## RegistrationClass () {            
      ns3::TypeId tid = type::GetTypeId ();             
      tid.SetSize (sizeof (type));                      
      tid.GetParent ();                                 
    }                                                   
  } Object ## type ## RegistrationVariable

在我们调用这个函数时,如:

NS_OBJECT_ENSURE_REGISTERED(type);

编译器会将分号前的文本NS_OBJECT_ENSURE_REGISTERED(type)替换为:

static struct Object ## type ## RegistrationClass     
  {                                                     
    Object ## type ## RegistrationClass () {            
      ns3::TypeId tid = type::GetTypeId ();             
      tid.SetSize (sizeof (type));                      
      tid.GetParent ();                                 
    }                                                   
  } Object ## type ## RegistrationVariable

也就是我们声明了一个结构体Object ## type ## RegistrationClass,同时其拥有构造函数Object ## type ## RegistrationClass (),我们同时定义了一个这个结构体的变量Object ## type ## RegistrationVariable,并同时执行了上面定义的构造函数。