C Abstract——const

本文详细解析了C++中const关键字的三种用法:限制指针本身不可更改、限制指针所指内容不可更改以及同时限制两者。通过具体示例展示了不同情况下const的应用及其效果。

const关键字是一个用来限定变量不被修改的修饰符,在一定程度上可以提高程序的安全性和可靠性。


1)int *const p

const修饰p,故而直接对p进行赋值修改是非法的,p只能指向初始化地址,修改p指向的内容是合法的。

    #include <stdio.h>  
    #include <stdlib.h>  
    int main(int argc, char* argv[])   
    {  
        int a=1;  
        int b=2;  
        int *const p=&a;  
        printf("p address %x, p value is %d.\n", p, *p);  
        *p=b;//p=&b是非法的,因为p的地址不能被改变  
        printf("p address %x, p value is %d.\n", p, *p);  
    }

2)int const *p

const修饰的对象是p指向的内容,所以对*P的赋值是非法的,但通过修改p指向的地址是允许的。

    #include <stdio.h>  
    #include <stdlib.h>  
    int main(int argc, char* argv[])   
    {  
        int a=1;  
        int b=2;  
        int const *p;  
        p=&a;//*p=a是非法的  
        printf("p address %x, p value is %d.\n", p, *p);  
        p=&b;  
        printf("p address %x, p value is %d.\n", p, *p);  
    } 

3)int const* const p

const同时修改了p和p指向的内容,所以p和*p在初始化时就被确定了,任何赋值都是非法的。

    #include <stdio.h>  
    #include <stdlib.h>  
    int main(int argc, char* argv[])   
    {  
        int a=1;  
        int b=2;  
        int const *const p=&a;//必须在声明时初始化  
        printf("p address %x, p value is %d.\n", p, *p);  
    } 


可以根据这个CameraMode基类去做吗?// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #include "Engine/World.h" #include "GameplayTagContainer.h" #include "TFCameraMode.generated.h" class AActor; class UCanvas; class UTFCameraComponent; /** * ETFCameraModeBlendFunction * * Blend function used for transitioning between camera modes. */ UENUM(BlueprintType) enum class ETFCameraModeBlendFunction : uint8 { // Does a simple linear interpolation. Linear, // Immediately accelerates, but smoothly decelerates into the target. Ease amount controlled by the exponent. EaseIn, // Smoothly accelerates, but does not decelerate into the target. Ease amount controlled by the exponent. EaseOut, // Smoothly accelerates and decelerates. Ease amount controlled by the exponent. EaseInOut, COUNT UMETA(Hidden) }; /** * FTFCameraModeView * * View data produced by the camera mode that is used to blend camera modes. */ struct FTFCameraModeView { public: FTFCameraModeView(); void Blend(const FTFCameraModeView& Other, float OtherWeight); public: FVector Location; FRotator Rotation; FRotator ControlRotation; float FieldOfView; }; /** * UTFCameraMode * * Base class for all camera modes. */ UCLASS(Abstract, NotBlueprintable) class UTFCameraMode : public UObject { GENERATED_BODY() public: UTFCameraMode(); UTFCameraComponent* GetTFCameraComponent() const; virtual UWorld* GetWorld() const override; AActor* GetTargetActor() const; const FTFCameraModeView& GetCameraModeView() const { return View; } // Called when this camera mode is activated on the camera mode stack. virtual void OnActivation() {}; // Called when this camera mode is deactivated on the camera mode stack. virtual void OnDeactivation() {}; void UpdateCameraMode(float DeltaTime); float GetBlendTime() const { return BlendTime; } float GetBlendWeight() const { return BlendWeight; } void SetBlendWeight(float Weight); FGameplayTag GetCameraTypeTag() const { return CameraTypeTag; } virtual void DrawDebug(UCanvas* Canvas) const; protected: virtual FVector GetPivotLocation() const; virtual FRotator GetPivotRotation() const; virtual void UpdateView(float DeltaTime); virtual void UpdateBlending(float DeltaTime); protected: // A tag that can be queried by gameplay code that cares when a kind of camera mode is active // without having to ask about a specific mode (e.g., when aiming downsights to get more accuracy) UPROPERTY(EditDefaultsOnly, Category = "Blending") FGameplayTag CameraTypeTag; // View output produced by the camera mode. FTFCameraModeView View; // The horizontal field of view (in degrees). UPROPERTY(EditDefaultsOnly, Category = "View", Meta = (UIMin = "5.0", UIMax = "170", ClampMin = "5.0", ClampMax = "170.0")) float FieldOfView; // Minimum view pitch (in degrees). UPROPERTY(EditDefaultsOnly, Category = "View", Meta = (UIMin = "-89.9", UIMax = "89.9", ClampMin = "-89.9", ClampMax = "89.9")) float ViewPitchMin; // Maximum view pitch (in degrees). UPROPERTY(EditDefaultsOnly, Category = "View", Meta = (UIMin = "-89.9", UIMax = "89.9", ClampMin = "-89.9", ClampMax = "89.9")) float ViewPitchMax; // How long it takes to blend in this mode. UPROPERTY(EditDefaultsOnly, Category = "Blending") float BlendTime; // Function used for blending. UPROPERTY(EditDefaultsOnly, Category = "Blending") ETFCameraModeBlendFunction BlendFunction; // Exponent used by blend functions to control the shape of the curve. UPROPERTY(EditDefaultsOnly, Category = "Blending") float BlendExponent; // Linear blend alpha used to determine the blend weight. float BlendAlpha; // Blend weight calculated using the blend alpha and function. float BlendWeight; protected: /** If true, skips all interpolation and puts camera in ideal location. Automatically set to false next frame. */ UPROPERTY(transient) uint32 bResetInterpolation:1; }; /** * UTFCameraModeStack * * Stack used for blending camera modes. */ UCLASS() class UTFCameraModeStack : public UObject { GENERATED_BODY() public: UTFCameraModeStack(); void ActivateStack(); void DeactivateStack(); bool IsStackActivate() const { return bIsActive; } void PushCameraMode(TSubclassOf<UTFCameraMode> CameraModeClass); bool EvaluateStack(float DeltaTime, FTFCameraModeView& OutCameraModeView); void DrawDebug(UCanvas* Canvas) const; // Gets the tag associated with the top layer and the blend weight of it void GetBlendInfo(float& OutWeightOfTopLayer, FGameplayTag& OutTagOfTopLayer) const; protected: UTFCameraMode* GetCameraModeInstance(TSubclassOf<UTFCameraMode> CameraModeClass); void UpdateStack(float DeltaTime); void BlendStack(FTFCameraModeView& OutCameraModeView) const; protected: bool bIsActive; UPROPERTY() TArray<TObjectPtr<UTFCameraMode>> CameraModeInstances; UPROPERTY() TArray<TObjectPtr<UTFCameraMode>> CameraModeStack; };
最新发布
11-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值