6

I was wondering if it was possible to restrict access to certain functions or objects declared in a namespace exclusively to other classes in the same file. For instance,

//myheader.h

namespace stuff
{
  int repair(firstObj obj);
  int doSomethingElse();
  privateObj obj;
}

In this case, I want the function doSomethingElse() and the object obj to be accessible to classes declared in this file only.

Is there some keyword I could use to restrict access?

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
user1553248
  • 1,184
  • 2
  • 19
  • 33
  • 1
    It makes perfect sense (consider a pimpl idiom or private helper functions) and is possible with anonymous namespaces. – Mark B Aug 02 '12 at 17:56

4 Answers4

13

use an unnamed namespace:

namespace stuff
{

  int repair(firstObj obj);

  namespace 
  {
    int doSomethingElse();
    privateObj obj;
  }

}

From here [emphasis mine]:

Unnamed namespaces are a superior replacement for the static declaration of variables. They allow variables and functions to be visible within an entire translation unit, yet not visible externally. Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit.

See also this related question: Unnamed/anonymous namespaces vs. static functions


Edit: Just noted that it's a header file we're talking about:

In this case, I want the function doSomethingElse() and the object obj to only be accessible to classes declared in this file.

Then you shouldn't declare these methods and objects in a public header file in the first place, but rather in the specific implementation file: If other shall not use them, other should not even know they'd exist.

And then use an unnamed namespace to effectively restrict the reachability (e.g. if someone would accidentally provide another declaration using the same identifier).

If you leave it in the header file, this could happen: anonymous namespaces and the one definition rule (if the header is included in more than one translation unit):

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
  • 1
    But I wouldn't put that in a header, like the OP seems to want. – Bo Persson Aug 02 '12 at 18:27
  • So if you want to restrict access in a header only library, you have no options? – jiggunjer Jul 24 '15 at 16:49
  • @jiggunjer I think the common approach in this case is to use a named namespace instead. Preferably one whose name makes it clear that you should not access it as an API user. Admittedly, it's just obfuscation then, no actual restriction. – moooeeeep Jul 24 '15 at 18:58
  • I made a util class with only private methods, then added friends from my file. – jiggunjer Jul 25 '15 at 07:41
5

Yes, put it in an anonymous namespace:

namespace stuff
{
    int repair(firstObj obj)

    namespace
    {
        int doSomethingElse();
        privateObj obj;
    }
}
Mark B
  • 95,107
  • 10
  • 109
  • 188
2

declare them as static this means that they will only be accessible within that file.

John Corbett
  • 1,595
  • 10
  • 19
  • 3
    According to C++11, using the `static` keyword is now un-deprecated. It was realized that C would never remove this use, so C++ will have to keep it as well. – Bo Persson Aug 02 '12 at 18:29
  • You can use static functions from outside the file in c++. – jiggunjer Jul 25 '15 at 07:43
0

You can use keyword static.

namespace stuff
{
 int repair(firstObj obj)
 static int doSomethingElse();
 static privateObj obj;
}

Thus the object and the function can be accessed only in the current file.

Maksim Skurydzin
  • 10,301
  • 8
  • 40
  • 53