Why write this
After using Unreal for a while, everyone will definitely find that whether it is ue4 the source code of various modules from your own, or the source code of certain plugins on the internet, or the source code of ordinary game modules, you can almost see that under each module's source code directory, there are more or less the three folders: Public, Private, Classes. At first, I didn't know why UE4 divided the code this way, and since the tutorials taught this, I just followed along, putting all the header files in the Public folder and all the implementation files in the Private directory.
Until one day, I saw a more bizarre code structure, which is that the Private folder contains both header files and implementation files. As a serious obsessive-compulsive disorder patient, I really couldn't stand this, so I had to figure out what these Public, Private, and Classes folders really are.
Detailed Introduction
After some searching, I learned that these three folders, apart from the Public folder, the other two folders have no special significance.
Classes Folder
The Classes folder used to be special because in older versions of the Unreal Engine, all declarations of UObject and derived classes had to be written in the Classes folder. However, this restriction no longer exists. If you compare and observe the recent versions of Unreal, you will find that the new features and various examples and tutorials that Unreal has added have completely abandoned the Classes folder. Therefore, if you still see a folder structure with Classes, it is probably written under an old version of Unreal. In future code, you can completely forget that this ever existed.
Public Folder
The current role of the Public folder under the module source code is that when the module itself is depended on by other modules, the path of the Public folder of that module will be automatically added as an 'include path' by other modules. For example, if you have a module A, and A/Public/ has two header files code1.h and code2.h; then I have a module B, and I add module A to thePublicDependencyModuleNameslist, at this point, I can directly include various header files under module A's Public folder in the code of module B, such as#include "code1.h".
Private Folder
The Private folder has no special role or significance. But it has become a convention to be used in conjunction with the Public folder. This will be explained below.
Separation of Public/Private Folders
Why use the separation of Public/Private folders? The main benefit of separating files this way is encapsulation. If you write a module that needs to be used by others, you only need to put the header files required for using this module in the Public directory and put its implementation files in the Private directory. If there are other functions' header filesand implementation files that do not need the user to care about or modify, they can all be placed in the Private directory. This way, users only need to look at what is in the Public folder and do not need to delve into your implementation.
Some Notable Points
Therefore, the convention in Unreal is:
It is not that header files are placed under Public and implementation files under Private, but rather that Public contains header files that need to be exposed to others, while other header files and implementation files that do not need to be exposed to others are placed in the Private directory.
The Private folder has no special significance; it is just habitually used in conjunction with Public, and it is not mandatory. You can use Pipixia to store private files as well.
Game modules (non-plugin modules) generally will not be depended on by other modules because game modules are basically the 'users' of all other existing modules. Therefore, you will see that the source code structure of some game modules online does not adhere to the convention of Public/Private separation. For game modules, this is acceptable but not recommended, as your game module may have a corresponding editor module, which generally needs to depend on your game module.

