user965369
user965369

Reputation: 5733

Control access in namespace?

I have writen a static library that consists of a major namespace. How do I control access to classes within that namespace? For example, if I wanted all classes inside to be 'private' and only a few interface functions to be accessable. Any Ideas? Can I do something like this:

namespace{
    public:
        void startSomeProcess();
    private:
    // global variables
        class Priv{};
}

Upvotes: 1

Views: 570

Answers (3)

Alok Save
Alok Save

Reputation: 206526

There are no access specifiers for a Namespace, You cannot do that.

Access specifiers are only for a class/structure.

If you do not want to expose certain classes do not put them in the header file which you expose to the users,If users cant see a class exists, they won't be using it.

"NmspPublic.h" to share with others

namespace Nmsp {
    void startSomeProcess();
}

"NmspPrivate.h" to keep internally

#inlcude "NmspPublic.h"
namespace Nmsp {
        class Priv{};
}

Upvotes: 4

drdwilcox
drdwilcox

Reputation: 3951

You cannot place access specifiers in a namespace. What you could do is create a class in the namespace and place static methods inside the class.

class AccessControl {
public:
    static void startSomeProcess();

private:
    class Priv {};
};

And use friend specifiers in Priv to control access.

Upvotes: 0

cyco130
cyco130

Reputation: 4934

Moving the "private" prototypes out of the header file and declaring the functions as static should do it.

Upvotes: 0

Related Questions