I particularly like the simplicity of using STL containers in the straightforward way.
I have never really figured out how to get the Boost library working on my dev platforms, in fact I don’t think I’ve even tried. I guess you could say I am just trying to delay the inevitable since Boost is clearly a helpful library that I should be using.
But my question is essentially the same as this topic: How to initialise a STL vector/list with a class without invoking the copy constructor
I have std::list<ExpensiveClass> mylist; and I just want a function that pushes a new instance into the list and calls the default constructor, rather than copying it from a temporary stack instance of it. In the other topic there was mention of move constructors. I looked them up and quite frankly it does nothing but strike fear into my heart. Two ampersands!!
Would it work if I just made an array of ExpensiveClass objects? ExpensiveClass *mylist = new ExpensiveClass[20]; Does this call the constructor 20 times?
Seems to me I should just use boost:ptr_list.
Inserting an object into a container invokes the copy constructor on that object. There’s really no way around that (yet), hence why pointer containers are used for large objects: it’s dirt cheap to copy a pointer.
If you choose to use a container of smart pointers, you can either use one of the Boost pointer containers or you can use an STL container of
shared_ptrs.To answer your question about:
The default constructor for
ExpensiveClassis called 20 times: once for each element of the array.