Link to home
Start Free TrialLog in
Avatar of dartslca
dartslca

asked on

specifying which function to call...

I have a function in my code which has the same name as a function in another header file I am including.  I want to call the other function within my function.  Anyone know how to do this?  I believe in C++ you would do this with the :: operator, but this needs to be programmed in C.

Thanks,

Darts
Avatar of TheMadManiac
TheMadManiac

Sorry to say, but you can not have 2 functions with the same name in a project unless one of the functions is static (limited to the source file it is in).

I suggest renaming your function.

Tam
Avatar of Paul Maker
you will have to rename as C does not support method/function overloading.

Paul
Avatar of dartslca

ASKER

is there a way to write/compile a function in c++ or another language and then include it in a C program??  The original program has to be compiled in C, but my function can be written in anything, as long as it can be read by the C program and it can include a header file that has the function i am trying to add code to.
you can compile the .C program using your c compiler

then in the header file (you do use one dont you? ;) you simply use:

extern "C" void myfunction(void);

with the return type and arguments the way you want it. The "C" part tells the C++ compiler that it should NOT mangle those names (C doesnt mangle). Now the C++ compiler can find the functions.

Tam
erm, the object file generated from the c compiler needs of course to be linked with the C++ program :)
so are you saying that I can supposedly, write my function in c++, which supports function overloading, and specify which function to use when I call it in my code(my function or a function found in a header file) and then in the original C program, include my function in a header file, compile my function in a c++ compiler, and compile the original program with a C compiler with the .o file from the c++ compiler linked in.  or does it have to be the other way around, compile it in a c++ compiler and link in the C main program(?).  Not sure if this will work cause the main program is written has to be compiled in C, can it?

Thanks,
Darts
You will need to compile the .c and .cpp files with their own compiler, then you need to link it with the linker that came with the c++ compiler. If you use a compiler that differentiates between .c and .cpp files and supports porjects just stuff them all in the project and the environment will do the rest.

You can then still not call the c++ functions with the same name as a function in your .C file, but you can call the function from the .C file from your c++ source (and it can overload it internally).

This is for backwards compatibility to 'old' C programs. It is not to interface new .C programs with .c++ programs.

In fact, due to the name mangling your .C source would not even KNOW the c++ functions.

Tam
Tam,
So that doesn't seem like it would work with what I have.  How about this...
I add a static function to the main C program and have that function call a function with a different name that is called thru a header and then that function can have the extra code needed and then call the original function(which then there will only be one, because the other one is static, so it won't be seen).  Really messy, but it seems like it would work.  do you see any flaws in this?

thanks,
Darts
i.e.
include "header.h"  //my function that calls the real func
include <realheader.h> //real function

static samenamefunc();

main()
{
     samenamefunc();
/*it would call the samenamefunc() in this file instead of the samenamefunc in realheader.h because of the compiling rules, right? */
}

static samenamefunc(){
     newsamenamefunc();
/* this func calls another function that is in header.h and inside this function, samenamefunc() is called and this would call the the samenamefunc() in realheader.h and not the one in the main program file. right? */
}

This would work right?
Darts
ASKER CERTIFIED SOLUTION
Avatar of TheMadManiac
TheMadManiac

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
I got it to work...
this is what I did:
MyStuff.h:
int myFunction();

MyStuff.c:
#include <RealStuff.h>
int myFunction(){
/* do stuff I need it to do */
  realFunction();//calls the real function from RealStuff.h
}

MainProg.c:
#include "MyStuff.h"
#include <RealStuff.h>

static int realFunction();

main(){
/* the program stuff I can't touch with a
bunch of realFunction() calls */
}
static int realFunction(){
  myFunction();
}

This seems to work if you can follow it.  Thanks for all the help Tam.  I will accept one of your comments as the answer.  
thanks again,

Darts
Thanks again for all the help.