Tuesday, July 14, 2009

Is it dangerous to mix the C with the C++ language.?

I'm a beginner C++ programmer, i'v noticed that there are a lot of C tutorials that allow the programmer to use the win32 API without any extra support. What i was thinking, is it possible to use objects and classes and incorprest that into a C but use a C++ complier.

Is it dangerous to mix the C with the C++ language.?
Yes, you can mix the two. C++ is a superset of C *but* there are some subtle differences that can cause a headache now and again.





One big item no one here has mentioned is that because C++ supports function overloading it "mangles" function names. If you are using a unit compiled by a C compiler you must declare the header file with extern "C" scope. In essence this tells the C++ compiler not to mangle the functions names in the C++ manner but to use the C naming convention. Doing this will allow your program to link properly.





Example:





extern "C"


{


#include "some_C_header.h"


}
Reply:You can combine ANSI standard C and C++ since C++ is simply 'enhanced C'. The result is C++ code
Reply:Yes C++ is an extension of C so ANYTHING you see that is done in C WILL work in C++ ... its not dangerous whatsoever to mix the two simply because they are one and the same (assuming you use C++, since it is a superset of C)... You mention win32 APIs and i've used those within C and in C++ without any problems at all. In fact you can even not bother with objects and classes all together (making it simply a C program, for the most part) and still compile it fine as C++. C++ gives you extensions that you have at hand, it's just a matter of whether you want to use them that differentiates C and C++.
Reply:You can download a free version of Microsoft Visual C++ at the URL below. It will recognize the difference between C and C++, based on the extensions of the file names. It has built-in facilities for handling the windows API.





Yes, you can mix C and C++, but remember that most callable routines in C++ are inside of an object. So you will need to tell your C code how to get "inside" the objects, probably by treating them as "structs". Usually it is not necessary to use C if you are using C++, since C++ can do everything that C can.
Reply:Absolutely not, C++ is a superset of C, which means everything contained within in C you can do. There are a few differences. For example, in C++, main must return an int. Also, structures are declared slightly different, but you can still use the C way. Win32 API is written in C, so it should be no surprise that it works. MFC is the Win32 API wrapped in C++ classes. The important thing to realize though, when using C header in C++. Normally you include %26lt;string.h%26gt;. C++ headers leave off the extension. So many people will think %26lt;string%26gt; equates to %26lt;string.h%26gt;. This is not true. All C libraries should begin witth a 'c'. So %26lt;string.h%26gt; equates to %26lt;cstring%26gt; and %26lt;stdlib.h%26gt; to %26lt;cstdlib%26gt;, etc.


No comments:

Post a Comment