tridev
asked on
can I CALL c++ METHODS FROM A c PROGRAM ?
HI,
can I CALL c++ METHODS FROM A c PROGRAM ?
THANKS in advance.
can I CALL c++ METHODS FROM A c PROGRAM ?
THANKS in advance.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
C++ mangles functionnames to code parametertype, number of parameters and so on to the function name. - That is the reason why function names looks so cryptic if the linker detects unresolved external functions.
But to switch of this mageling you can declare a function as
extern "C" Returntype Func(Parameter1Type P1 ,Parameter2Type P2,...,...)
in that case you only can use the features C provides.
Polimorph. functions are not possible for extern "C" declared functions. But
these functions now resides inside the C++ part they can call real C++ functons.
So you are able to write wrapper functions for the real C++ functions with full support to C++ functions
example:
Add(int Var1,int Var2, int* Result)
{
*Result=Var1+Var2
}
Add(double Var1,double Var2, double* Result)
{
*Result=Var1+Var2
}
extern "C" AddInt(int Var1,int Var2, int* Result)
{
Add(Var1,Var2,Result);
}
extern "C" AddDouble(double Var1,double Var2, double* Result)
{
Add(Var1,Var2,Result);
}
you can call AddInt or AddDouble fron C Code but you can't call Add directly from C
The example above makes no sense
because you can use + to do adding but it shows what I mean