What is const ?!!

If you're talking about const function in a C++ class header like this:
void SomeFunction(int32 Foo) const;
It means that the function can't modify any member variables of the class. These functions are often used for "getter" functions (just need to return a value). This constraint is enforced at compile time, so it's a good way to make sure your functions are modifying your classes when you don't intend them too.



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 constkeyword 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! :-)

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.)

댓글

이 블로그의 인기 게시물

About AActor!!! "UObject" has no member "BeginPlay"

UNREAL Android build information

Shader informations nice blog ~ ~