-5

This question arose while studying NS-3 codes. There is a for loop as below

enter code here

for (NetDeviceContainer::Iterator i = periDevice.Begin ();
                                i != periDevice.End ();
                                i++)
{
  (*i)->GetObject<BleNetDevice> ()->GetLinkLayer ()->SetAdvInterval (Time("1s"));

  (*i)->GetObject<BleNetDevice> ()->GetLinkLayer ()->SetRole (BleLinkLayer::ADVERTISER);
  (*i)->GetObject<BleNetDevice> ()->GetLinkLayer ()->SetAdvMode (BleLinkLayer::GENERAL_ADV);
}

What is the meaning of above code?
What is Iterator ? What is (*i)->xxx ?

Which c++ concept is used here.

Thank you in advance.

spectre
  • 113
  • 1
  • 9

1 Answers1

1

What is the meaning of above code?

It is a for loop over the objects in the periDevice container, calling the listed functions on each object.

What is Iterator ?

Iterator is a C++ concept (not an C++ exclusive concept) allowing one to iterate over collections. A collection in this context is a data-structure holding multiple objects of the same time (a NetDeviceContainer in your case).

For every iteration of the for loop, the iterator points to one object in the collection.

Some more details here: http://www.cplusplus.com/reference/iterator/

What is (*i)->xxx ?

i is an iterator. Assuming the iterator follows the usual standards, the * is overloaded and returns a reference to the containing object. This object seems to be of some type, for which the -> is defined (most likely a pointer) allowing you to access the "GetObject" function.

Which c++ concept is used here.

Difficult to say, what counts as a c++ concept. I would say:

  • for loops
  • Iterators
  • Pointers
  • Operator Overloading
  • Templates
Nathan
  • 7,099
  • 14
  • 61
  • 125