Here is some C++ example code that compiles and works fine:
class A
{
public:
A() {/* empty */}
private:
friend void IncrementValue(A &);
int value;
};
void IncrementValue(A & a)
{
a.value++;
}
int main(int, char **)
{
A a;
IncrementValue(a);
return 0;
}
What I would like to do, however, is declare IncrementValue() as static, so that it can't be seen or called from another compilation unit:
static void IncrementValue(A & a)
{
a.value++;
}
Doing that, however, gives me a compile error:
temp.cpp: In function ‘void IncrementValue(A&)’:
temp.cpp:12: error: ‘void IncrementValue(A&)’ was declared ‘extern’ and later ‘static’
temp.cpp:8: error: previous declaration of ‘void IncrementValue(A&)’
... and changing the friend declaration to match doesn't help:
friend static void IncrementValue(A &);
... as it gives this error:
temp.cpp:8: error: storage class specifiers invalid in friend function declarations
My question is, is there any way in C++ to have a (non-method) friend function that is declared static?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…