Link to home
Start Free TrialLog in
Avatar of tridev
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.


Avatar of Norbert
Norbert
Flag of Germany image

without any atribute - no
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
ASKER CERTIFIED SOLUTION
Avatar of Norbert
Norbert
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial