In Java they have package access specifier which enables the function to be used only by classes from this same "package" (namespace) and I see good things about it. Especially when design of models are in play. Do you think that something like this could be useful in C++?
Thanks.
Answer. private. public : A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. private : The private modifier specifies that the member can only be accessed in its own class.
It cannot be accessed from outside the package. If you do not specify any access level, it will be the default. Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
The access modifiers of C++ are public, private, and protected. One of the main features of object-oriented programming languages such as C++ is data hiding. Data hiding refers to restricting access to data members of a class. This is to prevent other functions and classes from tampering with the class data.
Remarks. The protected keyword specifies access to class members in the member-list up to the next access specifier ( public or private ) or the end of the class definition. Class members declared as protected can be used only by the following: Member functions of the class that originally declared these members.
As others have pointed out the usual way to do this is just by convention however C++ does provide friends which would allow you to specify that only the classes/functions you specify can access internals of a class e.g. private functions, this can be used to provide package like restrictions.
Mostly friends are avoided in C++ as they tend to increase coupling
There isn't really a direct equivalent because C++ isn't e.g. designed to cope with mobile code, :. access specifiers are a conveneince not a security feature anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With