如何在将对象添加到向量中时创建对象? [英] How can I create objects while adding them into a vector?

查看:124
本文介绍了如何在将对象添加到向量中时创建对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++向量。我想要向量持有可变数量的对象。

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

Visual Studio 2012给我一个错误:

Visual Studio 2012 is giving me an error:

Error: type name is not allowed

从此C ++代码:

#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");
}    

但这里是另一个看起来一样但是不工作的代码示例。 / p>

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.

推荐答案

要回答问题的第一部分,创建一个类型Player的对象,然后才能使用它。当你说 push_back(Player)时,意味着向播放器添加向量,而不是向量(这是你的意思)。

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

这篇关于如何在将对象添加到向量中时创建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆