Re: how to make static function to be friend of a class
Am 19.01.2011 21:38, schrieb nefedov:
I am writing some class as an interface to external C - library.
I have to use an interface function (signature of this function
is defined by C-library) and I want to hide this function using
the static specifier.
The simplest way for me is to define this function as a friend of
my class, but I have a problem in this place.
Consider an example:
//-------------------------------------------------------------
static int function(void* p); // It MUST be before class A ??
Let me add that for interaction of a C++ binary with a C library you
need a function with C linkage. Above function declaration declares a
function of C++ linkage with internal name linkage, though. What you ask
for is a C language linkage function with C name linkage:
extern "C" { static int function(void* p); }
class A {
private:
int a;
public:
A(int x) : a(x) {};
friend int function(void* p);
// error: storage class specifiers invalid in friend function
declarations
// friend static int function(void* p);
};
Note that the meaning of static is a different one than before. In the
context of a member function declaration the static specifier declares
a so-called non-static member function. This combination does not make
sense (Any member function of a class has always access to its class'
internals) and is not supported by the grammar.
In the context of a free function declaration the static specifier means
that this function has internal name linkage.
static int function(void* p)
{
A* pp = (A*)p;
return pp->a;
}
int main()
{
A a(7);
cout<< function(&a)<< endl;
return 0;
}
//-------------------------------------------------------------
This example is OK (g++ 4.4.5). But, I do not understand following:
1) Why the line:
// friend static int function(void* p);
does not work?
Why the storage class specifiers invalid in friend function?
What is the reason?
See above (There are two different meanings of static involved).
2) It seems I must to put declaration of function before class
definition.
It easy to do it in one file code, but how I should manage it in
the real
project? Should I write declaration of function in header of the
class?
It is weird.
If the friend function shall have internal linkage, there is no other
way than to declare the function previously. Otherwise the friend
declaration assumes external linkage which would not be satisfied in
above recommended situation.
There exists a slightly more complicated but IMO cleaner solution for
your problem that does not require to declare a static free function of
"C" linkage in the header. In this case you declare just a friend class
in the header like this:
class A {
private:
int a;
public:
A(int x) : a(x) {};
friend class Caller;
};
and in your implementation part you define function of internal name
linkage of C language linkage that just forwards to the Caller:
#include "A.h"
struct Caller {
static int fwd(void* p) { return static_cast<A*>(p)->a; }
};
extern "C" {
static int function(void* p) { return Caller::fwd(p); }
}
.... // Use function
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]