In C I can have a structure and some public functions declared in a header file, while some "private" functions can be declared as static in a source file. For example:
foo.h
typedef struct Foo {
...
} Foo;
void func1(Foo *foo);
foo.c
#include "foo.h"
static void func2(Foo *foo) {...}
void func1(Foo *foo) {...}
In this case func2
is linked internally. Is this possible with C++ class methods? If I write:
foo.hpp
struct Foo {:
void func1();
private:
void func2();
};
func2
will still be linked externally. Is there a way to make it internal retaining it inside the struct?
You can declare ordinary static functions in C++ just like in C, in any translation unit. But they will be ordinary static functions and not class methods.
A class's methods are not limited to a single translation unit. It is not uncommon for large class with many methods to get broken up into two or more different translation units. Private methods from one translation unit can certainly call other private methods in order translation units, of course. Therefore, their linkage must be external.
This is all that standard C++ gives you. Beyond that, there may be implementation-specific extensions one can use, if portability is not a concern. For example, gcc offers the visibility attribute. "internal" visibility appears to imply static linkage. I haven't verified that, but the attribute is noted as applicable to C++ code, too, not just C.