Jump to content
  • Advertisement
Sign in to follow this  
Assassin7257

Vector array of Objects

This topic is 2347 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

Is it possible to create a vector array with objects. Could I use that vector functions as well ?

vector<Object> myObject;

myObject.push_back(Object);


Could I use a base class as well, and let it store derived classes.

vector<baseClass> myVector;

myVector.push_back(DerivedClass);

Share this post


Link to post
Share on other sites
Advertisement

Is it possible to create a vector array with objects. Could I use that vector functions as well ?

vector<Object> myObject;

myObject.push_back(Object);



Yes. Note that the new Object may be copied into the vector (mind the rule of three).


Could I use a base class as well, and let it store derived classes.

vector<BaseClass> myVector;

myVector.push_back(DerivedClass);

[/quote]

Technically yes, although the derived objects will be sliced. (How can we guarentee random-access in an array, if an an array is guarenteed to be contiguous in memory but the elements in the array are variable in size? We would need to traverse the array much like a linked list in order to find the Nth element!)

</baseclass*></baseclass></object>

Share this post


Link to post
Share on other sites
Sure it's possible. You just need to ensure that your objects have value semantics (basically, it has a properly defined copy constructor and copy assignment operator) so that the object can be copied.

If you want to store polymorphic types, however, you need to store pointers to the baseclass instead, since runtime polymorphism is only available through pointers and references, but references are not value types so they cannot be stored in the vector which leaves you with pointers only.

Share this post


Link to post
Share on other sites
Loop over the objects in the vector and access them with the [ ]-operator, like you would do with a normal array of objects.

Share this post


Link to post
Share on other sites

Now say I wanted to output the array, how would I do that ?


Use iterators or one of the index operators that vector provides. Use size() to get the length of the vector... examples:

//Using iterators
for(auto itor = myvector.begin(); itor != myvector.end(); ++itor) {
itor->DoSomething(); //Invoke the "DoSomething()" method of the type contained in the vector.
}

//Using one of the index operators
for(size_t i = 0; i != myvector.size(); ++i) {
myvector.DoSomething(); //Same as above, except using indexing operator
//myvector.at(i).DoSomething(); // Uses the at() indexing method of the vector, which will throw if we go out of bounds.
// Not necessary here since we know we're in bounds due to the use of size().
}

Share this post


Link to post
Share on other sites

[quote name='Assassin7257' timestamp='1331768409' post='4922094']
Is it possible to create a vector array with objects. Could I use that vector functions as well ?

vector<Object> myObject;

myObject.push_back(Object);



Yes. Note that the new Object may be copied into the vector (mind the rule of three).
[/quote]
That's not a may that's a will get copied across , std::vector takes control over whatever you insert into it.

Share this post


Link to post
Share on other sites
Nobody has mentioned it, but this doesn't compile:
myObject.push_back(Object);

This does:
myObject.push_back(Object()); // Create a temporary default-constructed Object and push it into the vector

Share this post


Link to post
Share on other sites

[quote name='NightCreature83' timestamp='1331813684' post='4922243']
That's not a may that's a will get copied across

Only if the object does not have a move-constructor.
[/quote]

I dont work in C++11 yet as our current tool chains don't support it yet(thats a particular console), so when not using C++11 it will always cause a copy.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!