我可以在对象*** p中有一个指针向量吗?

时间:2021-06-17 17:00:38

So here's my question. I have a multidimessional array in wich i'm trying to put some objects deriven from class Block. I have this multidimessional array declared the following way:

所以这是我的问题。我有一个multidimessional数组,我试图把一些从Block类派生的对象。我有这个multidimessional数组声明如下:

Block ***arrTerrain;

where the first '' is for the lines, the second '' is for the collumns and the third is for the pointer to object inside the position (let's assume it's an object called FreeTerrain).

第一个''用于线条,第二个'用于柱子,第三个用于指向位置内对象的指针(让我们假设它是一个名为FreeTerrain的对象)。

I initialize it this way:

我这样初始化它:

this->arrTerrain = new Block**[a]; 

for(int i=0;i < a ;i++){
    arrTerrain[i] = new Block*[l];
}

for(int i=0;i<a;i++)
    for(int j=0;j<l;j++)
        arrTerrain[i][j] = new FreeTerrain;

I'm supposed to have soldiers in a given position (class soldier derives from block aswell). If i want to put 2 soldiers in the same position, i lose the pointer to the first one (obviusly). So i thought of having in each position a vector of Blocks or somesort but i find this difficult to implement.

我应该让士兵处于一个给定的位置(班级士兵也来自街区)。如果我想把2名士兵放在同一个位置,我会失去指向第一个的指针(obviusly)。所以我想在每个位置都有一个Blocks或somesort的向量,但我发现这很难实现。

Can anyone of you guys help me out?

你们中的任何人能帮助我吗?

Thanks in advance!!

提前致谢!!

1 个解决方案

#1


2  

Answer is yes, you can make a vector of (almost) anything.

答案是肯定的,你可以制作(几乎)任何东西的矢量。

Now the the implicit question is "SHOULD I make a vector of Object ***" and the answer is nooooooo.

现在隐含的问题是“我应该制作一个Object ***的向量”,答案是nooooooo。

First off, you should only use pointers when you have to. If you have an array of FreeTerrain that are all the same class, don't use new to them just make an array of FreeTerrain.

首先,你应该只在必要时使用指针。如果你有一个FreeTerrain数组都是同一个类,不要使用new来创建一个FreeTerrain数组。

And you shouldn't hold an array in a vector, that's just asking for trouble.

你不应该在一个向量中保存一个数组,这只是在寻找麻烦。

Overall you should just use a std::vector<std::vector<FreeTerrain>>.

总的来说,你应该只使用std :: vector >。

Edit: From the comments below, if you really need to use pointers (say, to have polymorphic references) use a unique pointer:

编辑:从下面的注释中,如果你真的需要使用指针(比如说,要有多态引用),请使用一个唯一的指针:

 std::vector<std::vector<std::unique_ptr<FreeTerrain>>> v;

#1


2  

Answer is yes, you can make a vector of (almost) anything.

答案是肯定的,你可以制作(几乎)任何东西的矢量。

Now the the implicit question is "SHOULD I make a vector of Object ***" and the answer is nooooooo.

现在隐含的问题是“我应该制作一个Object ***的向量”,答案是nooooooo。

First off, you should only use pointers when you have to. If you have an array of FreeTerrain that are all the same class, don't use new to them just make an array of FreeTerrain.

首先,你应该只在必要时使用指针。如果你有一个FreeTerrain数组都是同一个类,不要使用new来创建一个FreeTerrain数组。

And you shouldn't hold an array in a vector, that's just asking for trouble.

你不应该在一个向量中保存一个数组,这只是在寻找麻烦。

Overall you should just use a std::vector<std::vector<FreeTerrain>>.

总的来说,你应该只使用std :: vector >。

Edit: From the comments below, if you really need to use pointers (say, to have polymorphic references) use a unique pointer:

编辑:从下面的注释中,如果你真的需要使用指针(比如说,要有多态引用),请使用一个唯一的指针:

 std::vector<std::vector<std::unique_ptr<FreeTerrain>>> v;