Solved

Inserting C code into a C++ program

Posted on 2004-10-12
4
232 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:klopex
  • 2
4 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12287755
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
 
LVL 13

Assisted Solution

by:SteH
SteH earned 20 total points
ID: 12287785
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
 
LVL 12

Accepted Solution

by:
stefan73 earned 30 total points
ID: 12287849
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
 
LVL 3

Author Comment

by:klopex
ID: 12289310
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now