Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any guidelines on migrating from C to C++ [closed]

Tags:

c++

c

migration

I would like to know if you have some links to guidelines to migrating from C to C++.

I'm mainly interested in the constructions to check in the C code that could have problems the compiler can not detect? And what are the workarounds?

Note: Please don't answer with things the compiler is able to detect.

like image 408
Vicente Botet Escriba Avatar asked Mar 23 '11 00:03

Vicente Botet Escriba


4 Answers

One way to investigate this is just to read through some lists of incompatibilities between C and C++, and see which ones produce run-time issues rather than compilation issues. Such lists are plentiful, good starting places might be:

  • http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B
  • http://david.tribble.com/text/cdiffs.htm
like image 84
Tony Delroy Avatar answered Nov 11 '22 13:11

Tony Delroy


Do you mean learning C++ assuming knowing C? Or do you mean translating a C project to C++? If the latter, and if the project is of any significance size, I'd say don't do it. If the project has any momentum then it's suicide; it's a waste of time, effort, motivation and good spirit.

like image 41
wilhelmtell Avatar answered Nov 11 '22 13:11

wilhelmtell


The first two editions of Effective C++ focuses on C programmers coming to C++ running into a lot of pitfalls. I'd say go for Edition 2 of Effective C++ (its basically a rework of first edition).

Maybe my answer is invalid as there actually are a Effective C++ flag for GCC. Also with modern warnings for strict aliasing etc etc you are pretty close to perfect C++ if you get everything to compile with really all warnings enabled.

like image 29
stefan Avatar answered Nov 11 '22 14:11

stefan


You must unlearn what you have learned.

And more seriously -- the biggest mistake of teaching C++ is teaching it as a superset of C. C and C++ are two different languages with different approaches to solving problems. The syntax may be very similar, but the approach differs. And while most valid C programs are valid C++, this is just a side effect of building C++ on top of C.

Recompiling a C project with a C++ compiler doesn't make the project C++.

like image 21
Kornel Kisielewicz Avatar answered Nov 11 '22 13:11

Kornel Kisielewicz