Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] unclear code snippet , seeking the help of c++ people

Status
Not open for further replies.

knowledge_Seeker1

Newbie level 6
Joined
Jan 22, 2012
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,382
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 )

Code:
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
 
Last edited by a moderator:

Could anybody explain to me what is going on here ?

What parts of the posted code are you having trouble understanding?

What is your level of experience with C++ coding?

BigDog
 
What parts of the posted code are you having trouble understanding?

What is your level of experience with C++ coding?

BigDog

Thank you for your reply.

I know java, I never progammed with c++ .

---------- Post added at 00:38 ---------- Previous post was at 00:34 ----------

i have problem understanding the whole thing ! :(

---------- Post added at 00:41 ---------- Previous post was at 00:38 ----------

Let's take the first snippet :


Code:
class MyObject : public Object    //[COLOR="#FF0000"] here they are extending the Object class [/COLOR]
{
public:
static TypeId GetTypeId (void)    /[COLOR="#FF0000"]/ here they are creating a function that returns an object of type TypeId[/COLOR]
{
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;
};

Please use CODE tags when posting your code
 
Last edited by a moderator:

It appears you would greatly benefit from the study a C++ tutorial.

While both Java and C++ are both OOP languages, there are many significant differences.

Some of the differences in nomenclature:

Methods = Class Member Functions

Properties/Attributes = Class Data Members

I'll attempt to clarify a few points of the code snippet:

Code:
        class MyObject : public Object  // [COLOR="#FF0000"]Class Declaration indicating the class MyObject inherits the Member Functions and Data Members from class Object[/COLOR]
        {
           public:  // [COLOR="#FF0000"]Indicates the following are publicly accessible Member Functions and Data Members[/COLOR]
 
           static TypeId GetTypeId (void) // [COLOR="#FF0000"]Static Member Function which can be invoked without the instantiation of an object[/COLOR]
           {
               static TypeId tid = TypeId ("MyObject")
              .SetParent (Object::GetTypeId ())
              .AddConstructor<MyObject> ()
              .AddTraceSource ("MyInteger",
                                      "An integer value to trace.",
                                      MakeTraceSourceAccessor (&MyObject::m_myInt));
              return tid;
           }

            MyObject () {} // [COLOR="#FF0000"]Class Constructor [/COLOR]

            TracedValue<int32_t> m_myInt; // [COLOR="#FF0000"]Data Member[/COLOR]
        };

I would recommend studying some of the following C++ online tutorials:

The C++ Tutorial

C++ Language Tutorial

Programming Tutorials - C, C++, OpenGL, STL

You may also find the following group of interest:

Embedded C/C++ Programming

Having study Java, you should be able to quickly understand much of the C++ syntax and methodologies after studying one of the tutorials posted above

BigDog
 
Last edited by a moderator:
That was EXTREMELY helpful ! thanks !

I will definitely read one of those tutorials

Code:
could you clarify few more things ?

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;
}


It appears you would greatly benefit from the study a C++ tutorial.

While both Java and C++ are both OOP languages, there are many significant differences.

Some of the differences in nomenclature:

Methods = Class Member Functions

Properties/Attributes = Class Data Members

I'll attempt to clarify a few points of the code snippet:

Code:
        class MyObject : public Object  // [COLOR="#FF0000"]Class Declaration indicating the class MyObject inherits the Member Functions and Data Members from class Object[/COLOR]
        {
           public:  // [COLOR="#FF0000"]Indicates the following are publicly accessible Member Functions and Data Members[/COLOR]
 
           static TypeId GetTypeId (void) // [COLOR="#FF0000"]Static Member Function which can be invoked without the instantiation of an object[/COLOR]
           {
               static TypeId tid = TypeId ("MyObject")
              .SetParent (Object::GetTypeId ())
              .AddConstructor<MyObject> ()
              .AddTraceSource ("MyInteger",
                                      "An integer value to trace.",
                                      MakeTraceSourceAccessor (&MyObject::m_myInt));
              return tid;
           }

            MyObject () {} // [COLOR="#FF0000"]Class Constructor [/COLOR]

            TracedValue<int32_t> m_myInt; // [COLOR="#FF0000"]Data Member[/COLOR]
        };

I would recommend studying some of the following C++ online tutorials:

The C++ Tutorial

C++ Language Tutorial

Programming Tutorials - C, C++, OpenGL, STL

You may also find the following group of interest:

Embedded C/C++ Programming

Having study Java, you should be able to quickly understand much of the C++ syntax and methodologies after studying one of the tutorials posted above

BigDog
 
Last edited by a moderator:

could you clarify few more things ?


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.

Code:
Ptr<B> b = Create<B> ();

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:

firstvalue is 10
secondvalue is 20

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
 
Thank you Thank you thank you for all of your efforts, ! I FULLY understand the code now !

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.

Code:
Ptr<B> b = Create<B> ();

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
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top