What is the java "package private" equivalent in C++?

Viewed 1552

What is the java "package private" equivalent in C++ ? Java package privacy feature (where only classes within same package has visibility) is useful when providing APIs.

is there a similar feature for C++ (other than declaring other classes as "friend"s) ? to elaborate more, e.g. assume A.h and B.h are in same package (i.e. API lib) File : A.h

class A
{
public :
void doA();

private : 
 int m_valueA;
};

File : B.h

class B
{
public : 
void doB()

private:
int m_valueB;

}

What I want is,

public visibility : ONLY A::doA() and B::doB()

within package(i.e. API lib) : A should be able to access B::m_valueB and B should be able to access A::m_valueA. WITHOUT making each other "friend" classes.

1 Answers

c++ doesn't have packages as in java. But it does have namespaces, however namespaces is just that, a namespace. So it is a different beast.

A somewhat emulation could in some situations be an inner classes (classes within other classes) - since inner classes are considered members.

Besides that, there are header files and implementation(.cpp files) - in that sense you have units or modules that control what is actual visible (not just private but completely hidden - in particular if put into anon. namespace). This concept covers both just a single .h file and .cpp file or entire projects/libs/dlls that is more like a full package (and can choose what parts of API they expose through what gets 'shown' in their respective header files).

Related