Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package accessibility for function and/or class

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.

like image 494
There is nothing we can do Avatar asked Sep 14 '10 10:09

There is nothing we can do


People also ask

Which keyword can protect a class in a package from accessibility by the classes outside the package?

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.

What is package protected in Java?

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.

What are access modifiers CPP?

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.

What is protected access specifier?

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.


1 Answers

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.

like image 162
jk. Avatar answered Nov 15 '22 01:11

jk.