Is there a way to create a private class in C++?

Asked

Viewed 63 times

2

I wanted to create a class where it could be accessed in the library that I created, how do I declare the class as private and can be called only in the library?

1 answer

3


There’s no way to do this in C++.

In fact in no language is there definitive protection to avoid use. When there is some protective measure in access to some part of the code only works if the programmer does not insist. It is not protection to prohibit, but to inhibit. You have made the library available so you can access it.

In languages like C# that have an internal visibility level that inhibits external members from calling that class, nothing actually prevents it from being called. C++ doesn’t even have that.

If this is enough it is simple to solve. Do not provide the files with prototypes that are required for those who will use them to compile the code.

Actually not documenting the class publicly is usually enough to avoid external use.

Depending on the desired sophistication you can opt for the Pimpl language.

  • vlw I wanted a way to use in C++ because I have a program to do,and like when I put class constants to use only in a specific class of my algorithm,being a constant that could not be modified,then I thought about it,

  • It seems to me that now you are talking about something else completely different. But I didn’t understand anything.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.