Download

Generic Unreal-C++ questions

Since wouldn’t be nice to open several topic to ask dumb questions,I though would be best if I ask all in this one.

I was watching the battery collector tutorial,and there is this code:



protected:
	/**The pickup to spawn*/
	TSubclassOf<class APickup> WhatToSpawn;


What is the purpose of the word “class” inside the template? :confused:
In general,what’s the purpose of writing “class” other than when we first create that class in is own header file?

This is known as a ‘Forward declaration’. It works in this case because the class containing the WhatToSpawn variable doesn’t need to know the size of APickup - it just stores a TSubclassOf-type variable. If you were to have an APickup variable (i.e. not a pointer to one), your class would need to know the size of that APickup object in order to allocate enough space on the stack / in the class.

Any time the size of an object must be known, or methods/variables of that object are going to be accessed, that code must have the full definition of the class, i.e. by including the header file in which the class is declared. As a result, it’s common to forward-declare the class (or struct!) in the header file, and then include the relevant files in the corresponding .cpp file. This means that any other files that include this header don’t also include your Pickup.h file - they have to do it manually, since they don’t pick-up the includes from the .cpp file automatically.

I personally prefer to put a single forward declaration above the class definition, i.e. in your case:


#pragma once

class APickup;

class MyClass
{
    TSubclassOf<APickup> WhatToSpawn; // we don't need to know about the true size of APickup yet, but will probably need more information in the .cpp file
};

Hope that helps! Try this for further reading.

Thank you HateDread for the answer and the link, I have not yet fully wrapped my head around it but definitely is much more clear than it was before :smiley:

Next question is: it is possible to link nodes and stuff inside Blueprint and then have it “baked” into my .cpp and .h files? So that I can compare and study both and see what code generate what inside Blueprint