Need to Write C code from C++ Code?
I need to write my 4-5 .cpp and .h file to c code. In C++ code we have defined a class, constructor, destructor, function.
How to convert them in C code? Can somebody give me example of it so that i can implement it or provide link so that I can better explore it to my C code? How to implement all the func开发者_StackOverflow中文版tionality i mean constructor,destructor,functions of class in C ?
- convert all classes to data structures (typedefs)
- replace constructors and destructors with functions that use malloc/calloc and free and return/take pointers to your typedef'd structures
- eliminate polymorphism; if this is not possible then implement a message-dispatching function for each typedef (a big switch statement) and represent messages as constant integers
- alter all functions to take a pointer to the appropriate typedef
Note that typedefs do not support subtyping, so all inheritance will have to be converted to composition.
There are a lot of considerations and things that need to change, but I think what you want to do is emulate object oriented code in C.
Basically for each of your classes you need to take out the functions and leave only the data members inside the class/struct (you will rename class to struct and remove public/private/protected specifiers).
Then you need to add a first parameter to each of those functions which is a pointer to the struct to operate on.
Nearly all typical OO C++ code is just snazzy syntax around the old technique of creating libraries that created a "cookie" and required it to be passed into every call. So if none of the classes have virtual methods (no runtime dispatch) you should be able to do the following:
- Think of all class names as "facility" or "library" names.
- Put all data members of the class into a struct named classname (what the class used to be named)
- Turn all the methods into functions named classname_methodname.
- Add a pointer to the classname struct to the parameter list of all the functions (methods) for that class.
- Turn the constructors into a functions named classname_construct(# for overloads perhaps) and the destructor into a function named classname_destruct.
- Change all the method calls in other code from
objectname.methodname (...)
toclassname_methodname (objecname, ...)
. - This is the tricky part: You will have to put in code to call the destructors manually, since those are automagically called in C++.
Taking the example given in the comments:
class QtCommandData {
public:
QtCommandData(unsigned short net, unsigned short command,
unsigned long n_data_bytes, unsigned short flgs,
unsigned char* data = NULL);
QtCommandData();
~QtCommandData();
public:
unsigned char* m_pData;
int m_Async;
protected:
unsigned int m_nDataBytes;
unsigned int m_BytesAllocated;
protected:
int Fill_Trailer();
};
...becomes (I'll abbreviate the "facility name" from QtCommandData
to QtCD
):
typedef struct {
unsigned char* m_pData;
int m_Async;
unsigned int m_nDataBytes;
unsigned int m_BytesAllocated;
} QtCD;
QtCD_Construct(QtCD * handle,
unsigned short net, unsigned short command,
unsigned long n_data_bytes, unsigned short flgs,
unsigned char* data);
QtCD_Destruct(QtCD * handle);
QtCD_Fill_Trailer (QtCD * handle);
That's the general idea. Templates and dynamic dispatch and a few other things may throw you a monkeywrench or two, but I'm sure you are up to the challenge. :-)
For functions you also need to prevent the C++ compiler from name mangling if you want to link a C++ library to a C program, so you add blocks in your headers like
#ifdef __cplusplus
extern "C" {
#endif
/* Some functions here */
#ifdef __cplusplus
}
#endif
C does not directly support the OOP paradigm. Translating an OO design or code to C is a fairly simple mechanical process but not one that could not reasonably be fully covered here perhaps. There is at least one thread on the subject already: Can you write object-oriented code in C? with links to resources.
If your code uses the more complex C++ features such as generic programming, polymorphism and multiple inheritance, your work may be more difficult than it is worth. However C and C++ can interoperate fairly easy. Why would you not just provide a C wrapper around your C classes?
精彩评论