Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

c++ - How can I create objects while adding them into a vector?

I have a C++ vector. I want the vector to hold a variable number of objects.

Visual Studio 2012 is giving me an error:

Error: type name is not allowed

From this C++ code:

#include <iostream>
#include <vector>
using namespace std;

class testObject{
private:
   int someInt;
public:
   testObject(int a){ someInt=a; }
   void show() { cout<<someInt<<endl; }
};

int main()
{
    vector<testObject> testVector;
    cout << "Initial size: " << testVector.size() <<endl;

    for ( int i = 0; i < 3; i++ )
        testVector.push_back(testObject(3));
    cout << "New size: " << testVector.size() << endl;

    for ( int j = 0; j < 3; j++ )
        testVector[ j ].show();

    system("pause");
}    

But here's another sample of code that looks the same but it's not working.

void Dealer::setNumberOfPlayers( const int tNumber )
{
    for ( int i = 0; i < tNumber; i++ )
        vectorOfGamers.push_back(Player); // Player is a class that I created
}

Can I create vector to hold objects of Dealer, Bot and Player at the same time? How do I do that? As I know, all objects in vector should be of one type.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To answer the first part of your question, you must create an object of type Player before you can use it. When you say push_back(Player), it means "add the Player class to the vector", not "add an object of type Player to the vector" (which is what you meant).

You can create the object on the stack like this:

Player player;
vectorOfGamers.push_back(player);    // <-- name of variable, not type

Or you can even create a temporary object inline and push that (it gets copied when it's put in the vector):

vectorOfGamers.push_back(Player());    // <-- parentheses create a "temporary"

To answer the second part, you can create a vector of the base type, which will allow you to push back objects of any subtype; however, this won't work as expected:

vector<Gamer> gamers;
gamers.push_back(Dealer());    // Doesn't work properly!

since when the dealer object is put into the vector, it gets copied as a Gamer object -- this means only the Gamer part is copied effectively "slicing" the object. You can use pointers, however, since then only the pointer would get copied, and the object is never sliced:

vector<Gamer*> gamers;
gamers.push_back(new Dealer());    // <-- Allocate on heap with `new`, since we
                                   // want the object to persist while it's
                                   // pointed to

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...