1

In many other languages the default level of access for a class/struct and members is within the namespace scope, with public indicating that it is accessible outside of that namespace (or package, or library, or whatever they call their namespaces.)

In C++ I see I have the options public which makes it visible everywhere, private which is only for the class, and protected which is for this class and nested classes.

What about for only this namespace? That's often useful if you're writing a library that can access its own classes and their members, whilst restricting access by importing files.

Alasdair
  • 13,348
  • 18
  • 82
  • 138

1 Answers1

0

Your last remark is actually what the C++ namespace does.

Defining a class-attribute/-method whatever public makes it accessible outside of the class/class instance. But not outside of the namespace it's defined in. To make something accessible outside of it's current namespace you have to use some sort of qualified name or a using directive. See here for example.

Confusion may arise because per default e.g. without a explicit namespace definition scope around your class is defined in global namespace and thereby its public attributes/methods etc. are accesible anywhere in the global namespace.

FloWil
  • 432
  • 5
  • 15
  • But the importing file can still access class xyz with `namespace::xyz`, so it's not private. I mean to make it inaccessible to all but the current namespace, as `private` keyword does within a class scope. – Alasdair Apr 23 '21 at 08:13
  • @Alasdair Declare the intended-to-be-private class/struct/variable inside an unnamed namespace, i.e., one with no name, and it is only visible to the current translation unit, that is, the current `.cpp` file. – Casey Apr 23 '21 at 08:16
  • @Casey, that's a good idea, but can that be done with some members inside a class and not others? – Alasdair Apr 23 '21 at 08:19
  • Ok, if you want to do it like that I'm not sure namespaces are the right tool for that. Have you considered encapsulating the functionality within a pure virtual class? Many people use them to define *interfaces* in C++. – FloWil Apr 23 '21 at 08:23
  • 1
    @Alasdair You are confusing `class` and `namespace`, they are two different things in C++. `class`es define a type. `namespace`s prevent name collisions in code (and have the side effect of grouping code that logically would be related together, such as math-y types being in a `Math` namespace.) – Casey Apr 23 '21 at 08:23
  • @Casey I'm not confusing classes and namespaces. I'm saying that I want to have a member variable of a class that is accessible to code outside of the class but within the current namespace, but not accessible outside of that namespace. So if you import my library you cannot access that variable, but I can access it from another function outside of that class from within the same namespace. – Alasdair Apr 23 '21 at 08:24
  • @Alasdair Nevermind. I had a whole answer typed up before coming to the conclusion after trying it out that what you are asking does not work due to internal linkage properties, and how namespaces work; and, the fact that C++ is not other languages. Java is Java, C++ is C++, C# is C#. – Casey Apr 23 '21 at 09:21