tags:

views:

309

answers:

5

I need to write my 4-5 .cpp and .h file to c code. In C++ code we have defined class,constructor,destructor,function in the class.

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 functionality i mean constructor,destructor,functions of class in C ?

+4  A: 

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.

Brian R. Bondy
Can you please give me example ?
Neel Patel
What exactly do you need an example for? The task outlined in this answer is straightforward if you already know C++.
Chinmay Kanchi
+12  A: 
  1. convert all classes to data structures (typedefs)
  2. replace constructors and destructors with functions that use malloc/calloc and free and return/take pointers to your typedef'd structures
  3. 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
  4. 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.

Steven A. Lowe
About point 3, you can also build "manually" a vtable, initialized by the fake-constructors.
Matteo Italia
On point 2: Constructors/destructors have nothing to do with allocation/deallocation, but with initalization and finalization. That should probably be: 2.0) convert constructors and destructors into intialization and finalization functions 2.1) convert all new into malloc+call to inialization function, 2.2) convert all delete into call to finalization function + free, 2.3) ensure that finalization function is called before objects go out of scope for all classes that have non-trivial destructors
David Rodríguez - dribeas
Which actually is converting all the constructors to placement constructors.
Matteo Italia
@David the details are left as an exercise for the reader ;-)
Steven A. Lowe
+2  A: 

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 (...) to classname_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. :-)

T.E.D.
i have following class can u pls tell me how to do that ?
Neel Patel
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(); };
Neel Patel
+1  A: 

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
arsenm
I don't think that calling C code from C++ was what he was asking about.
Clifford
A: 

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: http://stackoverflow.com/questions/351733/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?

Clifford