Code:
int
main (int argc, char *argv[])
{
[COLOR="#FF0000"]
//they creating an array called myObject of type "MyObject", but what is the Ptr for ? [/COLOR]
Ptr<MyObject> myObject = CreateObject<MyObject> ();
// what does the[COLOR="#FF0000"] ->, and &IntTrace[/COLOR] mean ?
myObject->TraceConnectWithoutContext ("MyInteger", MakeCallback(&IntTrace));
myObject->m_myInt = 1234;
}
The following statement neither creates an array nor is it part of the Standard C++ Language:
Code:
Ptr<MyObject> myObject = CreateObject<MyObject> ();
Normally, the "new" operator is used to instantiate an Object from a Class in C++.
However, according to the NS-3 documentation all reference counted objects must be instantiate/created by using the Template Methods, Create<> or CreateObject<>.
Objects derived/inherited from the Object Class must use the CreateObject<> Template Method.
Code:
Ptr<WifiNetDevice> device = CreateObject<WifiNetDevice> ();
Objects NOT derived/inherited from the Object Class must use the Create<> Template Method.
You'll notice both these Template Methods make use of the "Smart Pointer Class" feature of NS-3, Ptr<>.
The Ptr<> Class provides a degree of memory management for dynamically created objects. Garbage Collection or Memory Management is not inherently included in C++ for dynamically created objects, therefore it must be dealt with manually, within the your code. The NS-3 Ptr<> takes care of Garbage Collection for objects created by both the CreateObject<> and Create<> Template Methods.
The concept of Pointers is a common stumbling block for novice programmers not familiar with C/C++.
C++ was in fact born from C. C has a particularly power feature, Pointers, which enables virtually any entity to be referenced/dereferenced directly or indirectly by its address in memory.
While this feature does exist, in a fashion, in other programming languages, the mechanism is concealed by a level of abstraction which prevents programmers from dealing directly and fully understanding it.
Which brings me to the second statement in question:
Code:
myObject->TraceConnectWithoutContext ("MyInteger", MakeCallback([COLOR="#FF0000"]&IntTrace[/COLOR]));
The "&" operator is referred to as the "reference" or "address of" operator.
Its complement, the "*" operator is referred to as the "dereference" operator.
The "->" operator is referred to as the "indirect membership," "deferenced membership" or "structure pointer" operator.
A variable or object data member which contains the memory address of an entity is referred to as a Pointer.
The Pointer in effect, points to the entity of interest.
The following code demonstrates the basic use of pointers and associated operators:
Code:
// pointers
#include <iostream>
using namespace std;
int main ()
{
int firstvalue = 5, secondvalue = 15;
int * p1, * p2; // declares two pointers to type integer
p1 = &firstvalue; // p1 = address of firstvalue
p2 = &secondvalue; // p2 = address of secondvalue
*p1 = 10; // value pointed by p1 = 10
*p2 = *p1; // value pointed by p2 = value pointed by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed by p1 = 20
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}
Code Output:
The topic of Pointers, their methodology and syntax, can be rather complex, far to complex to be adequately covered in even a lengthy forum posting.
As a matter of fact there have been entire texts written solely on the topic of Pointers.
I would strongly recommend you study the section on Pointers in one of the C/C++ tutorials previously posted.
Also study the section on C++ Templates, of which NS-3 appears to make frequent use.
I'm afraid there is no "easy" path to understanding C or C++ and therefore, the same can be said concerning the understanding and coding with NS-3.
Only through copious amounts of studying and practice coding can you achieve your goals.
BigDog