gpt4 book ai didi

c++ - 在 NS3 中遇到以下代码行。需要帮助理解它

转载 作者:行者123 更新时间:2023-11-28 04:10:36 27 4
gpt4 key购买 nike

Node对象声明为

class Node : public Object
{
public:
static TypeId GetTypeId (void);

它的定义是

TypeId
Node::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::Node")
.SetParent<Object> ()
.SetGroupName ("Network")
.AddConstructor<Node> ()
.AddAttribute ("DeviceList",
"The list of devices associated to this Node.",
ObjectVectorValue (),
MakeObjectVectorAccessor (&Node::m_devices),
MakeObjectVectorChecker<NetDevice> ())
.AddAttribute ("ApplicationList",
"The list of applications associated to this Node.",
ObjectVectorValue (),
MakeObjectVectorAccessor (&Node::m_applications),
MakeObjectVectorChecker<Application> ())
.AddAttribute ("Id",
"The id (unique integer) of this Node.",
TypeId::ATTR_GET, // allow only getting it.
UintegerValue (0),
MakeUintegerAccessor (&Node::m_id),
MakeUintegerChecker<uint32_t> ())
;
return tid;
}

我的问题是:

static TypeId tid = TypeId ("ns3::Node")
.SetParent<Object> ()

一旦我们声明了 tid 是什么,就没有行尾 ; 符号,接下来的几行以点 . 运算符开始.

    .SetParent<Object> ()
.SetGroupName ("Network")
.AddConstructor<Node> ()

在开始研究 NS3 之前,我对 OOP 进行了基础研究,但之前没有遇到过这种语法。

它是声明类 Node 的方法/属性的替代方法吗?

最佳答案

空格,包括换行符,在 C++ 中没有意义。 TypeId ("ns3::Node")创建一个临时对象。 .SetParent<Object> ()是在对象上调用的方法。显然,它返回对对象的引用,在该对象上 .SetGroupName()被调用,等等。

每个方法都会在临时对象上设置一些属性。完全配置后,用于初始化 static TypeId tid .

关于c++ - 在 NS3 中遇到以下代码行。需要帮助理解它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57898744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com