Unreal Engine 4 C + + Uclass constructors Error-prone analysis

Source: Internet
Author: User

    • Unreal Engine 4 C uclass Analysis of error-prone constructors
      • Generated_body
      • Generated_uclass_body

You typically see two macros in any class of Unreal Engine 4:

    • Generated_body ()
    • Generated_uclass_body ()

This is sometimes said in a tutorial:

This statement is not rigorous and does not fully explain the difference between the Uclass_body () and the BODY ().

Specific analysis:

First of all, why there are two macro definitions, mainly considering that, after inheriting the parent class, it is necessary to change the contents of the parent class, the constructor initialization is also the case.

Here's how the next two approaches are different:

1.generated_body ()

If Generated_body () is defined, then it means that I do not need to use the parent class's constructor, that is, I cannot use the declaration of the parent class directly, but when I need to implement it, I have to declare it myself or I will get an error.

    • MyCharacter.h
uclass () class  Mycharacter_api Amycharacter: public  acharacter {generated_body () Span style= "margin:0px; padding:0px; Color: #000088 ">public : Amycharacter (const  fobjectinitializer& Objectinitializer); };
  
 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

You can then implement this constructor of your own declaration in CPP and compile it.

    • MyCharacter.cpp
AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { }
  
 
    • 1
    • 2
    • 3
    • 4
    • 5

If you do not declare your own constructor, you will get an error:

MyCharacter.cpp (7): Error C2084: Function "Amycharacter::amycharacter (const Fobjectinitializer &)" already has a body
MyCharacter.h: note: See previous definition for ' {ctor} '

1.generated_uclass_body ()

If Generated_uclass_body () is defined, then it means that I use the constructor of the parent class, that is, I do not need to declare the constructor for myself, directly to implement the parent class declaration that constructor.

    • MyCharacter.h
uclass () class  Mycharacter_api Amycharacter: public  acharacter {Generated_uclass_ BODY ()}; 
  
 
    • 1
    • 2
    • 3
    • 4
    • 5

Then in the CPP file to implement, and do not need to declare in H, compiled through!

    • MyCharacter.cpp
AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { }
  
 
    • 1
    • 2
    • 3
    • 4
    • 5

If you also declare your own constructor, you will get an error:

uclass () class  Mycharacter_api Amycharacter: public  acharacter {Generated_uclass_ BODY () public : Amycharacter (const  fobjectinitializer& Objectinitializer); };
  
 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

This is the most common mistake.

Error C2535: "Amycharacter::amycharacter (const Fobjectinitializer &)": member function already defined or declared
Note: See the "amycharacter::amycharacter" statement

End.. Transfer from http://blog.csdn.net/qq_20309931/article/details/52964391

Unreal Engine 4 C + + Uclass constructors Error-prone analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.