Sign in to follow this  

C++ vectors and class constructors

This topic is 3479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I have a class with a constructor which assigns a random values some of its members. However, from what I can tell, when you allocate a vector of objects, it creates one object then assigns a copy of that object to every object in the vector. Perhaps some example source code will explain:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;

class Foo
{
public:
   Foo() : x(rand()%500) { }
   int getX() { return x; }

private:
   int x;
};

int main()
{

   srand( time(NULL) );

   Foo f[5];
   for ( int i = 0; i < 5; i++ )
   {
      cout << "x = " << f[i].getX() << ", ";
   }

   cout << endl;

   vector<Foo> f2(5);
   for ( int i = 0; i < f2.size(); i++ )
   {
      cout << "x = " << f2[i].getX() << ", ";
   }

   return 0;
}

The output of the above will be something like (obviously with randomness)
x = 214, x = 86, x = 258, x = 442, x = 391,

x = 262, x = 262, x = 262, x = 262, x = 262,

You can see that when using the vector, the constructor is only called once and then the values are copied into each element. Now, I know I can solve this by using an initialization function instead. Or perhaps by writing a copy constructor which re-randomizes the variable on copy (though I'd prefer not to do this as I feel a copy should merely copy the values, unless dealing with pointers). Is there some other method that I am overlooking or some trick to the STL vector to get what I want? Or do I just need to suck it up and use an initialization function?

Share this post


Link to post
Share on other sites
vector<Foo> f2;
f2.reserve(5);
for (int i=0; i<5; i++)
{
f2.push_back(Foo());
}

Share this post


Link to post
Share on other sites
Ah, wow. I do not believe I was aware I could create a new object by just calling the class constructor like that - which is odd considering how long I've been using C++... Guess you do learn something everyday with the language from hell. I did try doing the push_back method, but assumed I would need to use pointers and call new Foo() in the push_back call. However, I really did not want to abuse pointers like that.

That's the cleanest method I've seen for this. Thanks. :)

Share this post


Link to post
Share on other sites

Foo create() { return Foo(); }

std::vector<foo> f;
std::generate_n(std::back_inserter(f), 5, create);

Share this post


Link to post
Share on other sites

This topic is 3479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this