UCLASS(abstract, config=Game, BlueprintType, hidecategories=("Pawn|Character|InternalEvents"))
class ENGINE_API ACharacter : public APawn
{
GENERATED_UCLASS_BODY()
...
UPROPERTY(Transient)
uint32 bClientWasFalling:1;
/** If server disagrees with root motion track position, client has to resimulate root motion from last AckedMove. */
UPROPERTY(Transient)
uint32 bClientResimulateRootMotion:1;
/** Disable simulated gravity (set when character encroaches geometry on client, to keep him from falling through floors) */
UPROPERTY()
uint32 bSimGravityDisabled:1;
/**
* Jump key Held Time.
* This is the time that the player has held the jump key, in seconds.
*/
UPROPERTY(Transient, BlueprintReadOnly, VisibleInstanceOnly, Category=Character)
float JumpKeyHoldTime;
上面的代码来自UE4。他们似乎使用 uint32 一位位域而不是 bool 值。他们为什么要这样做?
最佳答案
一个独立的 bool
至少有一个字节长。处理器无法处理较小的单元。但是,我们都知道一个字节可以容纳 8 位/ bool 值,因此如果您有一个具有多个 bool 值的数据结构,则不需要每个字节都包含一个字节。他们可以共享一个字节。如果你有很多这样的结构,它可能会节省一些内存。
关于c++ - 为什么我应该使用 1 位位域而不是 bool 值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26470095/