Re: extern "C" and C++ conventions when passing parameters
Naven wrote:
i have a doubt whether the following is standard C++ or it "just"
compiles for whatever reason:
I created a DLL with an exported function
extern "C" void myFunction(std::vector<AbstractClass*> theVector)
{...}
The extern "C" i need for correctly unmangling the name
because i call the function with fnPtr =
GetProcAdress("myFunction").
Then I call this function from the main program, passing a
vector filled with objects of type MyDerivedClass1* and
MyDerivedClass2*.
At my big suprise, this code compiles and executes, virtual
functions of MyDerivedClassX are correctly recognized, i.e.
all works like standard C++.
As far as i know the extern "C" specifies C linkage for the
function, i.e. how can i pass objects to this function without
an error message?
Good question. I'd say that passing a full vector by value is
treading on awful thin ice. If nothing else, there's a good
chance it won't be destructed. But even declared as `extern
"C"', the code is compiled as C++ code. All that changes is the
calling conventions. So you can certainly use the vector in the
extern "C" functions, and anything which isn't actually passed
through the interface is pretty much guaranteed to work. And
passing a pointer to the vector is more or less guaranteed to
work, since an instantiated vector is, after all, a class (which
is the same thing as a struct, and you are guaranteed to be able
to pass structs). (I'd be leary of a reference, because a
compiler always could say that the C calling conventions don't
say how to pass references. The same thing goes for things like
pointers to members, or enum's whose underlying type is larger
than int. And of course, any object with a non-trivial
destructor.)
I've searched the net for samples or explications, but found
none. Can somebody tell me if this is standard C++ and allowed
and why?
First, it's allowed because if you break the rules, it's
undefined behavior, and the compiler can do anything it wishes.
Including making it work. Secondly, however, there's a lot more
guaranteed that you seem to think: the fact that a function is
`extern "C"' doesn't stop it from being full C++, so within the
function, there's no difference. Passing types which are
compatible in the two langages is also guaranteed to work, and
albeit quite indirectly, I think that passing pointers to any
data type or non-member function type is required to work.
--
James Kanze GABI Software
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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]