Inserting C code into a C++ program

I am working in C++.  We downloaded some C code from ITU that implements some compression algorithms.  I am rather new to C++ and I want to know what is the right approach to using C code in C++.

Should I translate it all into C++ classes?
Is there a common alternate approach?

All of the code will be compiled together on Linux using g++ 3.2.x.
LVL 3
klopexAsked:
Who is Participating?
 
stefan73Commented:
Hi klopex,
I think the most feasible approach would be to create C++ wrapper classes which call the C API.

By this, you have a nicely encapsulated compressor state, or can use exceptions, without the danger of a C->C++ migration. And additionally, you can easily exchange the C part when there is a new version.

Cheers!

Stefan
0
 
SteHCommented:
To use them as they are you need to change the function declaration to be extern "C":
in the header file add:

extern "C" {

.. all function prototypes.

}
0
 
SteHCommented:
Whether you need to translate it to C++ depends on your preferences and the needs of the application. Advantage of the C approach: code is tested, you don't need to do it (as thoroughly). C++ approach: full OO support, encapsulation. You have more control on how data is changed and who can use it in which way.
0
 
klopexAuthor Commented:
Thank You.  I had been considering writing the wrapper class so I wouldn't have to edit good code.  I think that is what I am going to do...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.