简体   繁体   English

何时在游戏开发中使用C ++指针

[英]When to use C++ pointers in game development

I've looked at a bunch of articles, and most tell the same story: don't use pointers unless you have to. 我看了很多文章,大多数人讲的是同一个故事:除非必须,否则不要使用指针。 Coming from a C#/Java background where memory is all managed, I have absolutely no idea when it's appropriate to use a pointer, except for these situations: 来自C#/ Java背景,其中内存全部被管理,我完全不知道什么时候使用指针是合适的,除了这些情况:

  • dynamic memory (like variable-size arrays) 动态内存(如可变大小的数组)
  • polymorphism 多态性

When else would I use pointers, especially in the context of gamedev? 什么时候我会使用指针,特别是在gamedev的上下文中?

"Don't use pointers, they're slow" doesn't make sense (at least not in C++). “不要使用指针,它们很慢” 没有意义 (至少在C ++ 中没有意义 )。

It's exactly like saying, "Don't use variables, they're slow" . 这就像是说“不要使用变量,它们很慢”

  • Did you mean, "Don't use dynamic memory allocation "? 你是说,“不要使用动态内存分配 ”?
    If so: I don't think you should worry about it right now. 如果是这样的话:我认为你现在不应该担心。 Write the code first, then optimize later. 首先编写代码,然后再进行优化。

  • Or did you mean to say, "Don't use raw pointers (ie of type foo* )", which would require new and delete ? 或者你的意思是说,“不要使用原始指针(即类型为foo* )”,这需要newdelete
    If so, that is good advice : You generally want smart pointers instead, like shared_ptr or unique_ptr , to manage objects, instead of dealing with raw pointers. 如果是这样,这是一个很好的建议 :你通常需要智能指针 ,比如shared_ptrunique_ptr来管理对象,而不是处理原始指针。 You shouldn't need to use new and delete in most C++ code, even though that might seem natural. 您不应该在大多数C ++代码中使用newdelete ,即使这看起来很自然。
    But they're still pointers under the hood! 但它们仍然是引擎盖下的指针!

  • Or did you mean something else? 或者你的意思是其他什么?

Addendum 附录

Thanks to the @bames53 below for pointing this out: 感谢下面的@ bames53指出这一点:

If passing a copy is an option (ie when you're passing small amounts of data, not a huge structure or an array that could be larger than a few registers, eg on the order of ~16 bytes), do not use pointers (or references) to pass data; 如果通过一个副本是一个选项 (即当你传递少量数据,而不是一个巨大的结构或数组可能比一些寄存器较大,例如顺序〜16个字节的), 使用指针(或参考)传递数据; pass by copy instead. 而是通过副本传递。 It allows the compiler to optimize better that way. 它允许编译器以这种方式更好地进行优化。

One primary use of pointers (although references are generally better when possible), is to be able to pass an object around without copying it. 指针的一个主要用途(尽管在可能的情况下引用通常更好),是能够在不复制对象的情况下传递对象。 If an object contains a lot of data, copying it could be quite expensive and unnecessary. 如果一个对象包含大量数据,那么复制它可能非常昂贵且不必要。 In terms of game development, you can store pointers in a container like a std::vector so you can manage objects in a game. 在游戏开发方面,您可以将指针存储在像std::vector这样的容器中,这样您就可以管理游戏中的对象。 For instance, you could have a vector that contains pointers to all enemies in the game so you can perform some "mass procedure" on them. 例如,你可以有一个包含指向游戏中所有敌人的指针的向量,这样你就可以对它们执行一些“大规模程序”。 It might be worthwhile to look into smart pointers , too, since these could make your life a lot easier (they help a great deal when it comes to preventing memory leaks). 考虑智能指针也许是值得的,因为这些可以让你的生活变得更轻松(它们在防止内存泄漏方面有很大帮助)。

The idea is that you don't want to have memory leaks, nor slow down the program while retrieving memory, so, if you pre-allocate the structures you will need, then you can use pointers to pass the structures around, as that will be faster than copying structures. 这个想法是你不想让内存泄漏,也不想在检索内存时减慢程序,所以,如果你预先分配你需要的结构,那么你可以使用指针传递结构,因为比复制结构更快。

I think you are just confused as to why pointers may be considered bad, as there are many places to use them, you just need to think about why you are doing it. 我觉得你很困惑为什么指针可能被认为是坏的,因为有很多地方可以使用它们,你只需要考虑你为什么这样做。

You don't need pointers for variable size arrays, you can (and usually should) use std::vector instead. 你不需要可变大小数组的指针,你可以(通常应该)使用std::vector You don't need raw pointers for polymorphism either; 你也不需要用于多态的原始指针; virtual functions work with smart pointers like unique_ptr . 虚函数与unique_ptr等智能指针一起使用。

In modern C++ you rarely need pointers. 在现代C ++中,你很少需要指针。 Often they will be hidden away inside resource owning classes like vectors. 通常它们会被隐藏在资源拥有的类中,如矢量。 To pass objects around without copying them you can use references instead. 要传递对象而不复制它们,您可以使用引用。 Most of the remaining uses of pointers are covered by smart pointers. 指针的大多数剩余用法都被智能指针覆盖。 Very rarely you will want a 'non-owning' pointer, in which case raw pointers are fine. 很少你会想要一个'非拥有'指针,在这种情况下原始指针是好的。

You'll learn the appropriate uses of pointers if you learn about the things to should use instead of pointers. 如果你了解应该使用的东西而不是指针,你将学习指针的适当用法。 So learn about RAII, references, and smart_pointers. 因此,了解RAII,参考和smart_pointers。

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

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