Download

Novice question about "class" variable define

hi guys, I am a new UE4 game programmer, I got a question when I watch the tutorial of UE4 C++, the presenter wanna have an arrow component on the tank so he type the code:
class UArrowComponent* arrowcomponent; something like that.
so my question is, since UArrowComponent is already been defined in other head file, why he still need a “class” to re-define that class?

anyone here?

It is not a redefinition it is a forward declaration letting the compiler know that it is a pointer by that type name. In the implementation file you still have to implement the correct header before you can use that type or it will let you know that you are trying to use an incomplete type.

Right,



class UArrowComponent* ArrowComponent


lets the compiler know that type of ArrowComponent is a pointer of “class UArrowComponent”, without it needing to know the full definition of ArrowComponent. Forward declarations are usually used in header files and it is good practice, because it reduces compilation time as the compiler doesn’t need to read the contents of ArrowComponent.h in every other file where you use ArrowComponent. You could see forward declarations as a simplified representative of the class, which is all the compiler needs to know except when you’re going to use the class’s contents (functions and member variables).