I initialize an array of Entity pointers and add entities like this...
我初始化一个实体指针数组并添加这样的实体......
Entity* ents[16];
Player* player = &Player(20, 24); // subclass of entity
Player* player2 = &Player(30, 30);
ents[0] = player;
ents[1] = player2;
I loop through them with 16 as the size since I already know it.
因为我已经知道了,所以我用16的大小来循环它们。
But I can't do anything with them... because some of them are not "valid". I can't run any methods on entities 2-16 because I didn't set what they are. Visual Studio keeps freaking out about an exception. So I try to add an isValid method to the Entity class, I even tried giving it a public boolean and set it to true in the constructor, but ultimately there's no way to check any data about one of these "invalid entities" without getting an exception. So I need to be able to find out if an entity is valid first before I try to do anything at all with it. But how?
但是我不能对他们做任何事......因为他们中的一些不是“有效的”。我无法在实体2-16上运行任何方法,因为我没有设置它们是什么。 Visual Studio一直在寻找异常。所以我尝试将一个isValid方法添加到Entity类中,我甚至尝试给它一个公共布尔值并在构造函数中将其设置为true,但最终没有办法检查任何关于这些“无效实体”之一的数据而没有获得例外。所以我需要能够先找出一个实体是否有效,然后才尝试对它进行任何操作。但是怎么样?
2 个解决方案
#1
6
No, you can't do anything with elements 2-16 because those are invalid pointers. You didn't even set them to NULL. They are just invalid pointers. You cannot call any function through them or anything else.
不,你不能对元素2-16做任何事情,因为那些是无效的指针。你甚至没有将它们设置为NULL。它们只是无效的指针。您无法通过它们或其他任何方式调用任何函数。
You could use a pointer value of nullptr
to indicate "unfilled" array positions but, honestly, you should be using a std::vector<std::unique_ptr<Entity>>
to store good pointers to good data and have no "unfilled" array positions at all. The vector will grow as needed.
您可以使用nullptr的指针值来指示“未填充”的数组位置,但老实说,您应该使用std :: vector
As it happens, even your so-called "valid" array elements are dangling pointers to temporaries and thus not "valid" in any way at all. I hate that Visual Studio allows this: by the rules of the standard, this program should not even compile.
碰巧的是,即使你所谓的“有效”数组元素也悬挂在临时指针上,因此根本不会“有效”。我讨厌Visual Studio允许这样做:根据标准的规则,这个程序甚至不应该编译。
#2
0
You need to make sure the uninitialized elements in the ents
array are distinguishable from the initialized values. For example:
您需要确保ents数组中未初始化的元素可以与初始化值区分开来。例如:
Entity* ents[16];
Player* player = &Player(20, 24); // subclass of entity
Player* player2 = &Player(30, 30);
ents[0] = player;
ents[1] = player2;
for (size_t i = 2; i < 16; i++)
ents[i] = 0;
// then elsewhere, you can use
size_t j = 12;
if (ents[j])
{
// do something with ents[j] because it has a valid pointer
}
But note that this is a simplified answer for your simple question. In general you should use std::vector
for a container rather than built-in arrays, and smart pointers rather than raw pointers.
但请注意,对于您的简单问题,这是一个简化的答案。一般来说,你应该使用std :: vector作为容器而不是内置数组,以及智能指针而不是原始指针。
#1
6
No, you can't do anything with elements 2-16 because those are invalid pointers. You didn't even set them to NULL. They are just invalid pointers. You cannot call any function through them or anything else.
不,你不能对元素2-16做任何事情,因为那些是无效的指针。你甚至没有将它们设置为NULL。它们只是无效的指针。您无法通过它们或其他任何方式调用任何函数。
You could use a pointer value of nullptr
to indicate "unfilled" array positions but, honestly, you should be using a std::vector<std::unique_ptr<Entity>>
to store good pointers to good data and have no "unfilled" array positions at all. The vector will grow as needed.
您可以使用nullptr的指针值来指示“未填充”的数组位置,但老实说,您应该使用std :: vector
As it happens, even your so-called "valid" array elements are dangling pointers to temporaries and thus not "valid" in any way at all. I hate that Visual Studio allows this: by the rules of the standard, this program should not even compile.
碰巧的是,即使你所谓的“有效”数组元素也悬挂在临时指针上,因此根本不会“有效”。我讨厌Visual Studio允许这样做:根据标准的规则,这个程序甚至不应该编译。
#2
0
You need to make sure the uninitialized elements in the ents
array are distinguishable from the initialized values. For example:
您需要确保ents数组中未初始化的元素可以与初始化值区分开来。例如:
Entity* ents[16];
Player* player = &Player(20, 24); // subclass of entity
Player* player2 = &Player(30, 30);
ents[0] = player;
ents[1] = player2;
for (size_t i = 2; i < 16; i++)
ents[i] = 0;
// then elsewhere, you can use
size_t j = 12;
if (ents[j])
{
// do something with ents[j] because it has a valid pointer
}
But note that this is a simplified answer for your simple question. In general you should use std::vector
for a container rather than built-in arrays, and smart pointers rather than raw pointers.
但请注意,对于您的简单问题,这是一个简化的答案。一般来说,你应该使用std :: vector作为容器而不是内置数组,以及智能指针而不是原始指针。