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.
Yes, this can be accomplished. You may restrict visibility by public, protected, private keywords, as well as source visibility.
Declared visibility is commonly understood.
Source visibility (if you're coming from Java) is a bit different.
Java: you have one file for an object interface/implementation. Cpp: you have one file for an interface. the implementation may lie in the interface, or one or more implementation (cpp, cxx) files.
If you use abstract classes/interfaces/functions apart from the public interface of the library, they are effectively hidden and not accessible (well, this is false if you have particularly nosy users who dump symbols -- then they may redeclare the interface, and link to the symbols but... that's obviously undefined territory for them). So you only need to make the symbols you like visible. It's a good idea to use a package/library naming convention to avoid linking errors - place the classes in a namespace reserved for the library's private implementation. Simple.
Would it be useful in C++... it's not bad, although I personally think there are higher priorities for the language.
example of source visibility:
/* 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;
}