While i am going through different examples in NS-3 ( network simulator) i came across a definition like this. I coudn't figure out what exactly this syntax means.
Ptr<Node> a = CreateObject < Node > ();
In some other cases they use similar syntax, but RHS is quite different.
HelperClass help;
Ptr< xxx > a = help.somethingrandom();
or they prefix const
before xxx
.
I guess this is a different way of creating objects in c++. But it is still confusing. Can anyone please elaborate whats happening ? Thanks in advance.
Assuming
Ptr
is some smart pointer class. It seemsCreateObject
is template function, with implementation that simply boils down to this:The idea is that the code is generic, it will work for any type. Using a function ensures no resources leak during multiple initializations, if a constructor happens to throw an exception.
The standard library has an equivalent
std::shared_ptr
/std::unique_ptr
with matchingstd::make_shared
/std::make_unique
functions.