When I write object-oriented code in C, I normally put the struct definition together with the public functions in a header file, and implement the public functions in a separate .c file. I give the static keyword to all functions that are "private" for this class, and implement them in the .c file as well. The public functions can then call the private functions belonging to the same class. The private functions cannot be called from outside, thanks to the static keyword, so GCC can optimize many of these functions. They are often inlined and the original function is entirely deleted from the output object file.
Now to my question: how can I do the same with C++ classes?
Let's say I have a header file:
class A {
int private_field;
int private_function();
public:
int public_function();
};
And my .cpp file:
#include <iostream>
#include "A.h"
int A::private_function() {
std::cin >> private_field;
return private_field;
}
int A::public_function() {
return private_function() + 4;
}
In the resulting object file, private_function is left as a separate symbol, and public_function calls private_function (not inlined). I would like to give private_function internal linkage, so the compiler can do the same optimizations as when I use C. I have tried with anonymous namespaces and static, but I can't get it to work as I would like to. How to do it correctly, is it even possible? I use GCC.
Internal Linkage: An identifier implementing internal linkage is not accessible outside the translation unit it is declared in. Any identifier within the unit can access an identifier having internal linkage. It is implemented by the keyword static .
To use internal linkage we have to use which keyword? Explanation: static keyword is used for internal linkage.
Internal linkage refers to everything only in scope of a translation unit. External linkage refers to things that exist beyond a particular translation unit. In other words, accessible through the whole program, which is the combination of all translation units (or object files).
Explanation: External Linkage-> means global, non-static variables and functions. Internal Linkage-> means static variables and functions with file scope. None Linkage-> means Local variables.
Class members never have internal linkage. The Standard says (section 9.3):
Member functions of a class in namespace scope have external linkage. Member functions of a local class have no linkage.
So if you want to create helper functions with internal linkage, you have to use a non-member.
static int private_function(A* pThis);
class A
{
int private_field;
friend int private_function(A* pThis);
public:
int public_function();
};
and then
#include <iostream>
#include "A.h"
static int private_function(A* pThis)
{
std::cin >> pThis->private_field;
return private_field;
}
int A::public_function() {
return private_function(this) + 4;
}
Please note this rule from the Standard (section 11.3):
A function first declared in a friend declaration has external linkage. Otherwise, the function retains its previous linkage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With