c++ - Ptr<Node> a = CreateObject < Node > (); -


while going through different examples in ns-3 ( network simulator) came across definition this. coudn't figure out syntax means.

ptr<node>  = createobject < node > ();   

in other cases use similar syntax, rhs quite different.

helperclass help;

ptr< xxx > = help.somethingrandom(); 

or prefix const before xxx.

i guess different way of creating objects in c++. still confusing. can please elaborate whats happening ? in advance.

assuming ptr smart pointer class. seems createobject template function, implementation boils down this:

template<typename obj> ptr<obj> createobject() {   return ptr<obj>(new obj); } 

the idea code generic, work type. using function ensures no resources leak during multiple initializations, if constructor happens throw exception.

the standard library has equivalent std::shared_ptr/std::unique_ptr matching std::make_shared/std::make_unique functions.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -