What public and private mean on C++ wizard?

Well I see that they are subfolder to the project code, but what will happen if I use one or the other? I mean, it is not enought with public and private of C++?

Also normally I see the tutorials just putting all the code in the main folder, not even using public or private or separating code in subfolders.

1 Like

We just put all our code in public. The one nice benefit of header in “public”, it is easier include them since you don’t need to include their directory path. For example, if you have a header in:

“Source/Game/Public/SubFolder/AnotherSubFolder/HeaderYouWantToInclude.h”

Instead of having to:


#include "SubFolder/AnotherSubFolder/HeaderYouWantToInclude.h"

You can just:


#include "HeaderYouWantToInclude.h"

No offsets, subfolders, etc. MUCH easier to move code files.

1 Like

hehe, been testing and found that if you select public in UE4 it puts cpp in private and h in public with the sspecific path you wrote.

I would (havent tested) assume that hitting private will send both cpp and h to the specified path but in private.

I wanted to delete a C++ class (NPCNative) from UE4 that was in the top level (not inside private or public) and I dont see a delete, after delete it on system with explorer, it is still show in UE4. And reference Veiwer shows a path like Scripts/mygame. Before delete it from there, I was getting source\mygame\NPCNative.h(6): fatal error C1083: Cannot open include file: ‘NPCNative.generated.h’: No such file or directory maybe using public/private is not compatible with making all source files in top dir???


Just found you need to close UE editor and clean and build after delete the class, if not, UE4 because reflection? will think it is still there or some like htat.

And also have read the little messages over the buttons, now I get it, also found this Gameplay Modules | Unreal Engine Documentation still dont see a easy way/button to create modules

1 Like

It’s convention I think. It’s just one way of organizing your C++ files to make it more obvious. I believe they use public to represent includes (ie. header files that allow you to expose the signature of your classes to other files) and private being mostly implementation files (ie. files you wouldn’t want to use as includes). In some cases I’ve seen people not use public/private at all and go with their own set up.

1 Like

There is a reason to use public and private folders. Using public exposes it. Using private hides it. There should be no reason code files that are not used in a public manner to be in the public folder. Just like you don’t make things public you don’t want exposed in c++ itself.

The public/private structure is important to learn and know. Granted it doesn’t change much for a project implementation. but for plugin modules, this matters immensely. In fact, allot of code people put in their game prj, should be made into a plugin module with logical private/public top level folders.

Since UE4 is an API it makes sense to make and use plugins and let your game use those plugins. It also makes sense to dictate what files go in what folder because not only does it simplify your include statements significantly, but it also is much much cleaner directory structure and helps you keep a mental model of large amounts of code. The most important part is that it will help indicate what files are interface vs implementation.

This is a very popular type of folder structure. Every place I’ve consulted and worked with uses this structure.

Edit i should say that this private folder are not special they are just folder name, you can use any name you want… Though, from what i learned public is a special folder name.

5 Likes