Unreal Engine 4.x Scripting with C++ Cookbook
上QQ阅读APP看书,第一时间看更新

Making a UCLASS – deriving from UObject

When coding with C++, you can have your own code that compiles and runs as native C++ code, with appropriate calls to the new and delete operators to create and destroy your custom objects. Native C++ code is perfectly acceptable in your UE4 project as long as your new and delete calls are appropriately paired so that no memory leaks are present in your code.

You can, however, also declare custom C++ classes, which behave like UE4 classes, by declaring your custom C++ objects using the UCLASS macro. The UCLASS macro tells the class to make use of UE4's smart pointers and memory management routines for allocation and de-allocation according to their smart pointer rules, which can be loaded and read by the UE4 editor automatically, and can optionally be accessed from blueprints.

Note that when you use the  UCLASS  macro, your  UCLASS  object's creation and destruction must be completely managed by UE4: you must use the  ConstructObject  function to create an instance of your object (not the C++ native keyword  new ), and call the  UObject::ConditionalBeginDestroy()  function to destroy the object (not the C++ native keyword  delete ).