Patrick_ Posted May 28, 2009 Share Posted May 28, 2009 Hi, I'm trying to convert a C++ virtual function declaration to C. I have no experience with C++ and am clueless as to what to do. Basically, I need to know how to convert something like this (C++): virtual int function_name(int arg1, int arg2) = 0; ... to C. Any ideas? Link to comment Share on other sites More sharing options...
0 Patrick_ Posted May 28, 2009 Author Share Posted May 28, 2009 Just a quick note as to what I'm doing... I've got an API that provides access via LoadLibrary() and GetProcAddress(). The only header file they provide is for C++. Anyway, I'm to call a certain function in the library, and pass along a class filled with those virtual function definitions. That function then fills the class with pointers to the functions in the library that should be called. Because of this, I thought I should just be able to do: typedef int (*fnFunc)(int arg1, int arg2); struct class { fnFunc function_name; } But when I try to call function_name and pass along the arguments, my program crashes. Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted May 28, 2009 Veteran Share Posted May 28, 2009 (edited) Converting C++ to C is non-trivial, because C has no support for object-orientation. The first option is to try to understand what the program does and rewrite it in a strictly procedural way. This would need the most work, but it'd be a real translation. In this case, for instance, you wouldn't try to write a "virtual" method in C; you'd try to find a C way for your program to do the same thing. The second option is to try to write C++ using only C constructs, or in other words, C++ that compiles in C. Search "object-oriented C" for tutorials on how to emulate classes and inheritance (such as this and this), and once you get the hang of it, the translation should be fairly mechanical. After all, C++ originally compiled as C code. I think you will need a solid understanding of C++ in order to do this however. Now if the C++ code makes any substantial use of templates, I'd say you're in for a lot of fun. :p PS " There's a guy who apparently implemented inheritance using macros, maybe that can be of use to you : http://www.codeproject.com/KB/cpp/OOC.aspx Edited May 28, 2009 by Dr_Asik Link to comment Share on other sites More sharing options...
0 hdood Posted May 29, 2009 Share Posted May 29, 2009 You cannot. A virtual member function is not the same thing as a function pointer and has a different size and contains things that don't exist in C, such as a hidden parameter with the pointer to the instance of the class it is a member of. Your best bet would be to create a proxy in C++ that contains functions callable from C (since the library is presumably binary only and can't be changed). Is there a reason you can't use C++? Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted May 29, 2009 Veteran Share Posted May 29, 2009 You cannot. A virtual member function is not the same thing as a function pointer and has a different size and contains things that don't exist in C, such as a hidden parameter with the pointer to the instance of the class it is a member of.It is possible, you just have to implement virtual tables yourself. :p http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html Link to comment Share on other sites More sharing options...
Question
Patrick_
Hi, I'm trying to convert a C++ virtual function declaration to C. I have no experience with C++ and am clueless as to what to do.
Basically, I need to know how to convert something like this (C++):
... to C. Any ideas?
Link to comment
Share on other sites
4 answers to this question
Recommended Posts