devxlogo

Improving the Usage of Dynamically Allocated Objects

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);
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist