Package Availability for Function and / or Class

In Java, they have a package access specifier that allows this function to be used only by classes from the same β€œpackage” (namespace), and I see good things about it. Especially when the design of the models is in the game. Do you think something like this might be useful in C ++?
Thanks.

+4
source share
6 answers

Yes, it can be done. You can restrict visibility to public, protected, private keywords, as well as source visibility.

Declared visibility is usually understandable.

Source availability (if you are using Java) is slightly different.

Java: you have one file for the object / implementation interface. Cpp: you have one file for the interface. the implementation may consist of an interface or one or more implementation files (cpp, cxx).

If you use abstract classes / interfaces / functions, in addition to the public library interface, they are effectively hidden and inaccessible (well, this is not true if you have especially curious users who reset characters, then they can reuse the interface and the symbol link, but ... which is obviously undefined territory for them). Therefore, you only need to make the characters that you like visible. It is a good idea to use the package and library naming convention to avoid binding errors. Put the classes in the namespace reserved for the private library implementation. Plain.

It would be useful in C ++ ... it's not bad, although I personally think that there are higher priorities for the language.

source visibility example:

/* publicly visible header file */ namespace MON { class t_button { protected: t_button(); virtual ~t_button(); public: typedef enum { Default = 0, Glowing = 1 } ButtonType; /* ... all public virtual methods for this family of buttons - aka the public interface ... */ public: t_button* CreateButtonOfType(const ButtonType& type); }; } /* implementation file -- not visible to clients */ namespace MON { namespace Private { class t_glowing_button : public t_button { public: t_glowing_button(); virtual ~t_glowing_button(); public: /* ... impl ... */ }; } } MON::t_button* MON::t_button::CreateButtonOfType(const ButtonType& type) { switch (type) { case Glowing : return new Private::t_glowing_button(); case Default : return new Private::t_glowing_button(); /* .... */ default : break; } /* ... */ return 0; } 
0
source

As others have pointed out, the usual way to do this is only by agreement, however, C ++ provides friends , which allows you to specify that only the classes / functions you specify can access the internal elements of the class, for example private functions, this can be used to provide such packages.

In C ++, friends are most often found, as they tend to increase communication

Actually there is no direct equivalent, because C ++ is not, for example, designed to work with mobile code :. Access specifiers are not a security feature in any case.

+2
source

Java package roughly:

  • Method of grouping source files
  • Creating Compressed Binary Files

In C ++, the first can be achieved by using namespace . It is not possible to emulate the latter using a language construct.

+1
source

In C ++, if you need it, use classes.

IMO, access rules are quite complicated as it is.

(Note that you can have classes that contain nothing but static member functions, effectively implementing what you want.)

0
source

you can provide a header file that should be "publicly available" for users of the "package". Materials that should only be used internally can have header files in a folder named "internal" or (package name). This does not prevent someone from actually including these files, but you are communicating with what he should not!

0
source

"a little" late, but in case other people come here ...;)

There is a solution for simulating packages in C ++: use the features of inner classes (also called helper classes) that can access any member of their outter class, even private ones.

Example:

 class Package { public: class Public; private: class Private; }; 
  • Any user can use public classes.
  • Only classes from the package can use private classes.

Here is a complete example (and its permalink ):

 #include <iostream> // Package.hpp class Package { public: Package () = delete; class PublicClass; private: class PrivateClass; }; // PrivateClass.hpp / cpp class Package::PrivateClass { public: PrivateClass () { std::cout << "PrivateClass only usable by Package classes.\n"; } private: void Forbidden () {} }; // PrivateClass.hpp / cpp class Package::PublicClass { public: PublicClass () { std::cout << "PublicClass usable from outside/inside Package.\n"; Package::PrivateClass privateClass; // privateClass.Forbidden (); // error: 'Forbidden' is a private member of 'Package::PrivateClass' } }; // main.cpp int main () { // Package package; // error: call to deleted constructor of 'Package'. Package::PublicClass publicClass; //Package::PrivateClass privateClass; // error: 'PrivateClass' is a private member of 'Package' return EXIT_SUCCESS; } // Output: // ------- // PublicClass usable from outside/inside Package. // PrivateClass only usable by Package classes. 
0
source

All Articles