r/cpp_questions • u/Right-Edge-5712 • 24d ago
OPEN Public and private classes
Is there such a thing as private / public classes in C++?
23
Upvotes
r/cpp_questions • u/Right-Edge-5712 • 24d ago
Is there such a thing as private / public classes in C++?
11
u/max_wen 24d ago
You can't make a class private but you can make a class with a private constructor
7
u/__christo4us 24d ago edited 24d 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.
4
u/alfps 24d ago edited 24d 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".
16
u/AKostur 24d 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 24d ago
Members and inheritance have access control (public, private, protected), classes themselves don't.
2
u/mredding 24d ago
You're asking about class level linkage, and yes, there's two ways to do it. The first is in an unnamed (anonymous) namespace:
The class with internal linkage has only translation unit scope. Typically you wouldn't put an anonymous namespace in a header, because then you'd duplicate the object in every translation unit the header is included in, which is going to wind up in the target binary as redundant object bloat. How many copies of the same member functions do you need to compile with?
The other way you can do it is within a function definition:
Not only does this have internal linkage, but only function level scope can resolve this symbol.
1
u/I__Know__Stuff 24d ago
Yes, nested types, including class definitions, have access control.
1
u/davidc538 24d ago
I think modules add this, but otherwise not really.
0
u/ActualFeeling5039 24d ago
If all the methods and variables are private, can that class be called more or less private?
0
u/ir_dan 24d 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/i_grad 24d 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.
So no. Kind of yes if you relax your definition of "private" quite generously, but canonically, realistically, and thought-provokingly, no.
0
u/n1ghtyunso 24d 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 24d 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).
-6
u/Other-Resort-5941 24d ago
Yes
-2
u/Lannok-Sarin 24d ago edited 24d 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.
0
u/[deleted] 24d ago
[deleted] — view deleted comment