开发者

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

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 6 years ago.

开发者_C百科 Improve this question

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.


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


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.


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.


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++.


Besides the things that you already know, I suppose, new/delete versus malloc/free etc. there is one important danger, namely C style casts. If your code has a lot of these (usually this is bad C code, then) they are difficult to find because of the syntax. But you should convert them all to the correct C++ style casts.

And then there are subtle things that the two just interpret differently, that are difficult to find and may bite you any time from one week to 10 years after you made such a transition.

Identifiers are such a thing. Try to find out if in C++ stat means the function or struct stat. Where in this example this isn't too bad, since probably the compiler will tell you. But there may be others hidden. sizeof expressions are a particular pitfall here, since they can be applied to an expression or to a type.

sizeof in general are something that you should check systematically. E.g sizeof('a') in C is the same as sizeof(int) and in C++ sizeof(char) (so 1). The same holds for enum constants versus enum variables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜