Compile errors occur when try to init const member vars in init list

I was following the Control Camera View in C++ tutorial.I think the two const vars defined in Tick() function could be defined in public section.So this is my code:

//CameraDirector.h

class HOWTO_AUTOCAMERA_API ACameraDirector : public AActor

{

GENERATED_BODY()

public:

float const timeBetweenCameraChanges;
float const smoothBlendTime;		

};

//CameraDirector.cpp

ACameraDirector::ACameraDirector():timeBetweenCameraChanges(2.0f), smoothBlendTime(0.75f)

{

// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;

}

when I compiled I got errors:
HowTo_AutoCamera.generated.cpp(70): error C2789: ‘ACameraDirector::timeBetweenCameraChanges’: an object of const-qualified type must be initialized
CameraDirector.h(30): note: see declaration of ‘ACameraDirector::timeBetweenCameraChanges’

Hi Surtur,

The error isn’t generated in your code, but in the auto generated code from the class. Epic uses a lot of magical macros to help with the creation of UObjects (and UStructs), and it is simply a limitation that you can’t use const variables in the class declaration (unless it is static or declared with Uproperty(EditDefaultsOnly) potentially, not sure about this one I’ve never tried it).

Hope that helps. Cheers,

Thank you for replying.
So I can’t declaration any const member var in any class?

You can declare them in classes that don’t use the GENERATE_BODY() macro no problem. An alternative would be to use a static const variable or a const function that returns your value.