quick DMA constructor question (c++)

Started by
5 comments, last by simon_brown75 11 years, 11 months ago
Hi,

Simple question - is it possible to create a dynamic array of objects while passing a (different) value the constructor of each of those objects?

Thanks,
Simon.
Advertisement
Guess you could 'reserve(...)' the space on the vector then push_back each item separately, with the correct constructor arguments.
Thanks, but I've never used vectors or the STL, so TBH I don't really know what that means. I was just wondering if there was a way to do it in the c++ lanaguage. Of course I can just make a member function do the same thing (and I've already implemented it like that), but I like elegant solutions, and the thought occured to me that it was odd that you don't seem to be able to pass a value to a constructor if you are creating a dynamic array of those objects, since there doesn't seem to be any syntax for doing that.
C++11 adds support for custom constructors that allow this sort of thing, but I'm not sure how widespread support is for it just yet.

Aside from that, your best bet is to call reserve() on the vector to get a bunch of memory allocated in one shot, then push_back() each item you want in the vector, as bradbobak described.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

You can do this with placement new, but it's considered dangerous, so usually a std::vector would be used instead ;)

Thanks, but I've never used vectors or the STL, so TBH I don't really know what that means.

In case it's still not clear:
#include <vector>
#include <iostream>

int main()
{
std::vector<int> v; // our vector contains ints
// If you want your vector to hold a different data type, just change int
// do your data type and push_back the correct values
// If memory reallocation is a problem for you, you can do:
v.reserve(20); // this step is unnecessary, but pre-allocates room for 20 ints

std::cout << "There are " << v.size() << " objects in v" << std::endl;

v.push_back(0); // Add these values to the vector
v.push_back(1);
v.push_back(3);
v.push_back(42);

std::cout << "Now there are " << v.size() << " objects in v" << std::endl;

// v now contains [0, 1, 3, 42]
for (std::size_t i = 0; i < v.size(); ++i)
{
std::cout << i << ": " << v << std::endl;
}
}


[font=courier new,courier,monospace]std::vector[/font] will grow in size as needed. [font=courier new,courier,monospace]reserve()[/font] may be used to grow (in memory allocation, not in size) the vector in one step, rather than let it gradually grow (in memory allocation, not size).

The [font=courier new,courier,monospace]size()[/font] of a [font=courier new,courier,monospace]vector[/font] is how many objects are in it, not how much memory it has allocated (it will allocate extra memory when it needs to so that it doesn't have to allocate new memory every time you say [font=courier new,courier,monospace]push_back()[/font]).

If instead of [font=courier new,courier,monospace]int[/font]s, you want objects of type [font=courier new,courier,monospace]Foo[/font] that take an [font=courier new,courier,monospace]int[/font] in the constructor, you can do:

std::vector<Foo> v;
v.push_back(Foo(0));
v.push_back(Foo(100));
// etc...


And again, if you already know how many [font=courier new,courier,monospace]Foo[/font]s you want to put in the vector, you can use [font=courier new,courier,monospace]reserve()[/font] to preallocate the memory in one step rather than having the [font=courier new,courier,monospace]vector[/font] gradually allocate memory. The vector will automatically allocate more memory as necessary, and [font=courier new,courier,monospace]reserve(X)[/font] can be used to tell the vector that it's going to need enough memory for [font=courier new,courier,monospace]X[/font] number of objects, and it can allocate all that memory in one step rather than gradually as you [font=courier new,courier,monospace]push_back()[/font] new [font=courier new,courier,monospace]Foo[/font]s.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Thanks guys.

This topic is closed to new replies.

Advertisement