x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Class declaration into a header?

Hello, I've seen the following code in a EnemyManager.h file:

 #include "CoreMinimal.h"
 #include "CoreMinimal.h"
 #include "GameFramework/Actor.h"
 #include "EnemyManager.generated.h"
 
 class ABaseEnemy;
 
 
 UCLASS()
 class CPPINHERITANCE_API AEnemyManager : public AActor
 {
     GENERATED_BODY()
 
 ...
 
 private:
     TSubclassOf<ABaseEnemy> GetRandomEnemyClass() const;
 
 ...

I don't understand what this does

 class ABaseEnemy

mean. Because if you just do a

 #include "ABaseEnemy.h"

it works perfectly too.

What am I missing here?.(I'm also new to C++)

Thanks

Product Version: UE 4.18
Tags:
more ▼

asked Feb 13 '18 at 10:13 AM in C++ Programming

avatar image

Kyrax80
22 1 3 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

If you include a header file, you copy everything in that file at the position where the include statement is. Just writing

 Class classname;

Means you are declaring the class, so you are telling the compiler „this class does exists“ and you would use this if you only need the class as a datatype in the file, because that way you are not including the entire head file.

more ▼

answered Feb 13 '18 at 12:14 PM

avatar image

Takao_
186 6 5 13

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

This is called forward declaration, you declare incomplete class for type to work in header file, later it is properly declared in different include in cpp file (note that true function of #include is pasting content of the file to another file and fact that header files alone are never compiled on there own they are used in compilation of cpp files) if type is really used in cpp file, otherwise you will get use of incomplete type error. This is to avoid circular dependency in header files which may cause issues, if possible you should avoid including header file other then one containing base class of class you declaring, and forward declaration is one of methods to avoid that.

You can read about it here:

https://wiki.unrealengine.com/Forward_Declarations

more ▼

answered Feb 13 '18 at 01:20 PM

avatar image

Shadowriver
35.8k 927 169 1105

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question