What does "const" mean in this case?

/** get aim offsets */
UFUNCTION(BlueprintCallable, Category=“Game|Weapon”)
FRotator GetAimOffsets() const;
Look at the code,I’ve never seen const use like this:at the end of the function,how does it mean?Does it mean the return value is const?I know it’s pretty basic but please just tell me!

Hey,

Here you go :

const after a function declaration means that the function is not allowed to change any class members (except ones that are marked mutable). So this use of const only makes sense, and is hence only allowed, for member functions.

To illustrate a bit more what’s going on internally. Declaring a member method results in a function declaration that takes an implicit this pointer as a first parameter. So a method int Foo::Bar(int random_arg) (without the const at the end) results in a function like int Foo_Bar(Foo* this, int random_arg), and a call such as Foo f; f.Bar(4) will internally correspond to something like Foo f; Foo_Bar(&f, 4). Now adding the const at the end (int Foo::Bar(int random_arg) const) can then be understood as a declaration with a const this pointer: int Foo_Bar(const Foo* this, int random_arg).

const before an argument in a function definition as in your example means the same as const for a variable: that the value is not allowed to change in the function body. (I highlighted the word definition here, since the same const keyword in the function declaration will not change the function type signature; see for instance this answer for an more detailed explanation.)

Source :

1 Like

This gets a little into the peculiarities of C++ and const, but basically it means the pointer to the object instance (the this) is const, like a normal variable might be const.
So in the const method you cannot change the instance members, ensuring the object’s state will be the same before and after the method.
This is usually done to make debugging easier.

You can override this immutability of the instance object by the mutable keyword, but it is very atypical.

Looking at this particular function, FRotator GetAimOffsets() const;, there must be no need to mutate the state of the class this method belongs to.
This makes sense because getting the aim offset probably includes some calculation, making it not quite a simple getter, but we would in no circumstances want the player’s state to be modified by the function while getting this value.
You could as a programmer simply not touch the state, but by putting the const in the method signature, you’re making a promise to not do so and the compiler will statically check this promise to some extent.

We might call this a “peculiarity” or at least a peculiar part of C++ because sometimes the effects of the const keyword can be unexpected.
You will not be able to get a full review of what it means for the function without a good bit of reading, but the above should be a good overview! :slight_smile:

This part is really brilliant:

To illustrate a bit more what’s going on internally. Declaring a member method results in a function declaration that takes an implicit this pointer as a first parameter. So a method int Foo::Bar(int random_arg) (without the const at the end) results in a function like int Foo_Bar(Foo* this, int random_arg), and a call such as Foo f; f.Bar(4) will internally correspond to something like Foo f; Foo_Bar(&f, 4). Now adding the const at the end (int Foo::Bar(int random_arg) const) can then be understood as a declaration with a const this pointer: int Foo_Bar(const Foo* this, int random_arg).

Thanks!Really patient!

i don’t think that’s how the c++ compiler will actually evaluate the statement, but that is an excellent way of visualising the situation that i might shamelessly steal in the future if you don’t mind.
note you will not be able to do something such as make a “mutable” function or use the preprocessor as if it was doing the above visualisation except for the explicit instance of the function being const.