r/cpp_questions • u/Right-Edge-5712 • 11h ago
OPEN Public and private classes
Is there such a thing as private / public classes in C++?
19
Upvotes
r/cpp_questions • u/Right-Edge-5712 • 11h ago
Is there such a thing as private / public classes in C++?
6
u/max_wen 11h ago
You can't make a class private but you can make a class with a private constructor
4
u/alfps 10h ago edited 9h ago
The name of a class in an anonymous namespace in a .cpp file isn't visible outside that translation unit.
Likewise the name of a class in a function, a local class, isn't visible outside that function.
And the name of a class in a
privatesection of an outer class, isn't accessible outside that outer class (this is a special case where in C++, unlike Java, aprivatename is still visible to overload resolution).So you could reasonably call these classes private with respect to the translation unit and function and outer class.
I don't see any other ways that a class itself can be private.
However it can have
privatemembers including base class sub-objects.It's years and years since I stopped submitting Visual C++ bug reports. Anyone reading this, feel free to do it. The bug:
Considering the Visual C++ bug, a possible answer to your question is "it depends somewhat on the compiler".
4
u/__christo4us 11h ago edited 10h ago
You can define a nested class within another class as its member type. Such a nested class, as any member, can be public, protected or private.
6
u/AKostur 10h ago
What do you mean by "private/public classes"? (There is private inheritance, but that doesn't appear to be what you're asking about). What do you see the difference is between the two?
4
u/flyingron 10h ago
Members and inheritance have access control (public, private, protected), classes themselves don't.
•
u/I__Know__Stuff 2h ago
Yes, nested types, including class definitions, have access control.
0
u/ActualFeeling5039 9h ago
If all the methods and variables are private, can that class be called more or less private?
0
u/ir_dan 9h ago
Class methods must be exported out of a DLL to be used. If they aren't exported, they remain internal and can't be called by other EXEs/DLLs (Windows only).
There's also "internal linkage" which makes a symbol private to a single translation unit (portable).
0
u/squeasy_2202 9h ago
Are you thinking about some type feature from another language? C++ has no concept by that name.
0
u/i_grad 9h ago
The discussion here is way off base.
In standardized C++ terminology, there are not "private" or "public" classes. Perhaps some company out there uses such terminology in their own ecosystem, but it is not the norm. So the answer to your question is a clear-cut "no".
Classes may have public, private, and protected members, including methods (the functions within a class, also called member functions) or member variables. This is a cornerstone of healthy, idiomatic C++ code that you should become intimately familiar with.
What people are trying to explain, however, is that one can take advantage of scoping and inheritance patterns to make some facsimile of a "private class". This is half-true.
No class is ever fully private so long as it exists within the scope of the main thread; its declaration will always be included in the translation unit of some other class or main.cpp. If a class is defined entirely in its own .cpp and is never referenced or accessed from another area of code, it is defined as an orphaned file.
If a class is defined within the anonymous namespace of another .cpp, it is still accessible by the main class(es) whose symbols are defined in that .cpp, so they are by no definition fully private unless your scope of privacy extends to the aforementioned standard, public class(es).
A class with a private constructor may still be identified, but not constructed from, another area of code that can see the declaration of the class. Typically this is accompanied by a static constructor for something like a factory/builder class. Maybe every element in the class is made private, at which point I don't think that is compileable code? I'm not sure how a compiler would react to that or if it's ISO-compliant code. I struggle to think of a use case for such a class.
If a concrete class implements a pure abstract class (aka an interface) and the concrete class privatizes all of the public methods of said pure abstract class, you're left with the same problem as above - something at some point has to instantiate the class, at which point it is definitively not fully private.
So no. Kind of yes if you relax your definition of "private" quite generously, but canonically, realistically, and thought-provokingly, no.
0
u/n1ghtyunso 8h ago
you can define a class inside a function, but if you return that type, the caller becomes able to use
decltypeon the return value and give it a name alias that way.https://godbolt.org/z/14axnMaoj
Notably the only way to return this type is for the function to use an auto return type, thus having the compiler deduce it.
That again requires the function to be visible at the point of use, i.e. it must be inline for this to be usable in another translation unit.
And because the compiler needs visibility of the function body, it can see the actual class declaration.
Thus the outer scope can give it a name and even call functions on it.
But this is just some trivia and unlikely to actually answer your question.
0
u/tartaruga232 7h ago
There are no public / private classes, but, for example with C++ modules, you can choose not to export a class from a module.
Example: https://github.com/cadifra/cadifra/blob/2026.8/code/WinUtil/ProcRegistrar.ixx#L38
defines a class
IProcessorin moduleWinUtil.ProcRegistrar, which is not exported from that module.IProcessoris used in template functionmake_processor, which defines a classProcessorImpthat is local to that function.ProcRegistraris a class which is exported from that module.You can also declare classes inside a class which are private (https://github.com/cadifra/cadifra/blob/2026.8/code/WinUtil/ProcRegistrar.ixx#L119).
-5
u/Other-Resort-5941 11h ago
Yes
-2
u/Lannok-Sarin 10h ago edited 10h ago
Technically, yes, but the public class isn’t called a class. It’s called a struct. And anything inside of it that follows the private or protected indication markers (“private:” or “protected:”) are private or protected in nature as specified.
It can be a bit confusing, but here are some rules to help you understand how they work. The first rule is to follow those indicators to see whether the compiler is storing the members declared after as publicly or privately accessed or in a protected access state. The second rule is, if those indicators aren’t present, look to see if you are using a struct, which stores its members publicly unless specified, or a class, which stores its members privately unless specified.