简体   繁体   English

使用前检查指针? 必要性和替代方法,C ++ UE4

[英]Checking pointers before using? necessity and alternatives, C++ UE4

I'm new to UE4, and I encountered the general pattern of checking each pointer before using it, for example: 我是UE4的新手,遇到了在使用每个指针之前检查它的一般模式,例如:

AActor *pActor = getOwner(); 
if(pActor){
   pActor->SOME_FUNCTION();
}

I'm aware to the fact that crashes are awful in terms of user experience, and that it is always advised to write code with maximum robustness, but are there well-known cases in which the pattern is really useful? 我知道这样的事实,即崩溃在用户体验方面很糟糕,并且始终建议编写具有最大鲁棒性的代码,但是在众所周知的情况下该模式确实有用吗? (for example in theoretically safe settings, such as a pointer set to GetOwner() ). (例如,在理论上安全的设置中,例如设置为GetOwner()的指针)。

And if so, are there any common alternatives for this? 如果是这样,是否有其他通用替代方法? (perhaps making use of smart pointers?) (也许利用智能指针?)

EDIT: In my question I wanted to get an answer regarding UE4 specifically, since I encountered a source relating to UE4 only, which advised me to check pointers, although I wasn't sure about its necessity and in which pattern. 编辑:在我的问题中,我想获得关于UE4的具体答案,因为我遇到了仅与UE4有关的源,建议我检查指针,尽管我不确定它的必要性和使用哪种模式。

Since this is tagged C++ , there is an obvious alternative for a pointer that must not be NULL : a reference. 由于这是用C++标记的,因此对于不能为NULL的指针,有一个明显的替代选择:引用。 (see basic considerations or technical details for more information). (有关更多信息,请参见基本注意事项技术细节 )。

If you stay with pointers, you have - by definition - to deal with the possibility that each and everyone of them is NULL . 如果您使用指针,那么按照定义,您必须处理每个指针均为NULL的可能性。


Update: A pretty silly example: 更新:一个很愚蠢的例子:

AActor & get_existing_owner() 
{
    AActor * pActor = getOwner(); 
    check(pActor);
    return *pActor;
}

Please note that this changes the behavior of your code in case getOwner actually returns nullptr some time (cf. UE Assertions ). 请注意,这会改变代码的行为,以防万一getOwner实际返回nullptr时候(参见UE Assertions )。 Then you could write somewhere else (without further checks): 然后,您可以在其他地方写(无需进一步检查):

get_existing_owner().some_function();

Obviously above get_existing_... checking method could be generealized / reused for other pointers. 显然,在get_existing_...以上,检查方法可以实现/重新用于其他指针。

If you are not absolutely guaranteed to receive a valid pointer from a function that returns a pointer, you need to check that pointer. 如果不能绝对保证从返回指针的函数中收到有效的指针,则需要检查该指针。 In the case of libraries that are black boxes (you don't own the code), you can perhaps take the word of the library author that the pointer will always be valid, but based on a comment above, that does not appear to be the case here. 如果库是黑匣子(您不拥有代码),则您可能会想到库作者的话,该指针将始终有效,但是基于以上注释,这似乎并没有这里的情况。

Interestingly enough, in the C++ Core Guidelines: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-gsl 有趣的是,在《 C ++核心指南》中: http : //isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-gsl

There is a template in the Guidelines Support Library called not_null<T> which can be used to encapsulate a pointer to provide the guarantee that the pointer is never NULL. 准则支持库中有一个名为not_null<T>的模板,可用于封装指针,以保证该指针永远不会为NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM