[Solved] Why aren’t all the variable in .cpp file in the .h file? [closed]


The purpose of this tutorial you pointed out is to create the class AFloatingActor, which can then be instantiated. The RunningTimevariable is part of the interface of the class and since it is defined as public

public:
    float RunningTime;

it can be accessed outside the class (look at public/private class members and methods).

Opposed to that, the FVector NewLocation and float DeltaHeight are local variables used only in the method AFloatingActor::Tick. Because they are used only in this member (function) and nowhere else in the class neither they are part of the class interface, there is no need for them to be class variables.

If they would be in the .h file (therefore a class variable) then they would be created when the class is instantiated and kept alive as long as the class is. But there is no need for that, therefore they are created when AFloatingActor::Tick is called, they do their part and then get destroyed since they are local variables of that method.

This is also the answer for your second question “What is special about data in the .h files?”.

To sum it up:
In the .h file of a class you write the class interface. It consist of

  • public variables/methods: this are the variables/methods of this class that are accessible outside the class
  • private variables/methods: this variables/methods are used only in this class, they can not be accessed from outside and are shared with all the methods in this class
  • protected variables/methods: look for example here SO: private vs. protected variables

In the .cpp file of a class the class implementation is written (implementation of each method).

For more information look for example here.

3

solved Why aren’t all the variable in .cpp file in the .h file? [closed]