On Sep 12, 5:34 pm, "AnonMail2...@gmail.com" <AnonMail2...@gmail.com>
wrote:
On Sep 12, 10:12 am, Anonymous <no.re...@here.comwrote:
I have a class that needs to be accesed by a C API. I need to expose
some private methods to the C API :
#ifdef __cplusplus
extern "C"
That should be ``extern "C" {''.
#endif
void peek(Object_Handle handle);
void poke(Object_Handle handle);
What's Object_Handle?
#ifdef __cplusplus}
And this should be two lines (probably got mangled somewhere in
propagation).
#endif
class ToBeFlattened
{
public:
//compiler barfs at the next two lines (mangled names?)
//I also cant use the extern C decoration ...
friend __stdcall void peek();
friend __stdcall void poke();
There is absolutely no way a C program can ever call these
functions. For several reasons.
ToBeFlattened();
foo();
bar();
private:
void take_a_peek() const
void poke_all_you_want();
};
You can only let C have access to global functions or static class
functions.
C can only access functions declared ``static "C"''. Global
functions that aren't ``static "C"'' can't be used, nor can
static class functions (which can't be ``static "C"'').
You can't let them have access to member functions - how would
C supply the instance of the class object that is implicit in
the calls to a member function?
That's the most obvious problem. There's also the fact that the
calling conventions may be different between C and C++. The
result is that the "linkage" of a function is part of its type,
and if a function written in C is declared:
extern "C" void f( void (*)() ) ;
then it can only be passed functions declared and defined:
extern "C" void g() {}
In other words, given:
extern "C" {
typedef void (*PtrCFnc)() ;
}
typedef void (*PtrCppFnc)() ;
PtrCFnc and PtrCppFnc are two distinctly different types, with
no implicit conversion from one to the other.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34