I cannot undestand thic c++ code



Dear All,

I am seeking the help of people who know c++ .

Could anybody explain to me what is going on here ?

( i am tryin to learn ns3 and these codes are taken from ns3 tutorial chap7 )

class MyObject : public Object
{
public:
static TypeId GetTypeId (void)
{
static TypeId tid = TypeId ("MyObject")
.SetParent (Object::GetTypeId ())
.AddConstructor<MyObject> ()
.AddTraceSource ("MyInteger",
"An integer value to trace.",
MakeTraceSourceAccessor (&MyObject::m_myInt))
;
return tid;
}
MyObject () {}
TracedValue<int32_t> m_myInt;
};


void
IntTrace (int32_t oldValue, int32_t newValue)
{
std::cout << "Traced " << oldValue << " to " << newValue << std::endl;
}


int
main (int argc, char *argv[])
{
Ptr<MyObject> myObject = CreateObject<MyObject> ();
myObject->TraceConnectWithoutContext ("MyInteger", MakeCallback(&IntTrace));
myObject->m_myInt = 1234;
}

your help is HIGHLY appreciated
Which statement is the first statement you don't understand and which line does it start on?
Thank you for replying !


Let's take the first snippet :


class MyObject : public Object // here they are extending the Object class
{
public:
static TypeId GetTypeId (void) // here they are creating a function that returns an object of type TypeId
{
static TypeId tid = TypeId ("MyObject") // they lost me from this point downward !
.SetParent (Object::GetTypeId ())
.AddConstructor<MyObject> ()
.AddTraceSource ("MyInteger",
"An integer value to trace.",
MakeTraceSourceAccessor (&MyObject::m_myInt))
;
return tid;
}
MyObject () {}
TracedValue<int32_t> m_myInt;
};
I'm assuming that TypeId("MyObject") returns an object with a function called SetParent
which returns an object with a function called AddConstructor which returns an object of type
TypeId to be assigned to tid. In other words, they just put this conglomerate all on separate
lines for readability:
1
2
static TypeId tid = TypeId("MyObject").SetParent(Object::GetTypeId ()).AddConstructor<MyObject>().AddTraceSource("MyInteger", "An integer value to trace.", MakeTraceSourceAccessor(&MyObject::m_myInt));
return tid;
Last edited on
wow ! things are becoming much clearer now ! thanks,

ok , how about this : MakeTraceSourceAccessor (&MyObject::m_myInt)) .. I dont understand what the argument is about.
TracedValue<int32_t> m_myInt; // what is this ?
};


int
main (int argc, char *argv[])
{
Ptr<MyObject> myObject = CreateObject<MyObject> (); // what is this ?
};
myObject->TraceConnectWithoutContext ("MyInteger", MakeCallback(&IntTrace)); // what is this ?
};
myObject->m_myInt = 1234; // what is this ?
};
}


thank you a LOT, your explanation is great so far
I'm assuming that MakeTraceSourceAccessor takes a pointer-to-member, because &MyObject::m_myInt gives a member pointer to the m_myInt member. Note that MyObject is a class, and the double colon :: is the scope resolution operator ;)
oh I see ! thank you :))
Topic archived. No new replies allowed.