Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Intermediate
Jul 28, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Improving the Usage of Dynamically Allocated Objects

Suppose you need to fill a container with pointers to objects instead of actual objects. Usually, you do this when you want to store objects of different types in the same container. To do that, you allocate the objects from the free-store using operator new. However, instead of using named pointers (or worse yet—auto_ptr), insert the elements as follows:

class Base {};
class Derived : public Base{};

vector < Base * > v;
v.push_back(new Derived);
v.push_back(new Base);
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date