C ++ How to declare a class as a local file

So, I know that static functions are functions that are local to the file. Therefore, they cannot be accessed from other files. Does this work for classes too? I read a ton of disagreement about how the static classclass does not declare to contain purely static members and methods (which is obvious), but I could not find anything that indicated whether it would declare the class locally accessible to the file area, as it is more logical.

If this is not the case, what about using the anonymous namespace I heard can also be used to declare local functions of a file?

+4
source share
2 answers

You can define a class in an unnamed namespace, for example,

namespace
{
    struct A {};
}

In this case, the class name will have an internal binding. That is, this is visible only in the compilation unit where it is defined, and all compilation units that include this definition will have their own class definitions.

As for the storage class specifier static, (7.1.1 Storage class specifiers )

5 The static specifier can only be applied to variable and function names and anonymous unions.

+11
source

Does this work for classes too?

No. there is no such “static” keyword for a class.


" " (Foo) () cpp. , , - ...

X.cpp:

// Foo declared
class Foo
{
public:
   //...ctor
   //...dtor
   // etc.       
}

// ... Foo defined (implemented)
Foo::etc() { ... }


// implementation of X - only X has access to Foo.

X.cpp.

And File X.hpp does not reference Foo.

(, , ), - .


SO Q & A. , , , .

.

+1

Source: https://habr.com/ru/post/1661641/


All Articles