Converting C source to C++

前端 未结 11 2115
迷失自我
迷失自我 2021-01-30 16:52

How would you go about converting a reasonably large (>300K), fairly mature C codebase to C++?

The kind of C I have in mind is split into files roughly corresponding to

11条回答
  •  無奈伤痛
    2021-01-30 17:10

    Here's what I would do:

    • Since the code is 20 years old, scrap down the parser/syntax analyzer and replace it with one of the newer lex/yacc/bison(or anything similar) etc based C++ code, much more maintainable and easier to understand. Faster to develop too if you have a BNF handy.
    • Once this is retrofitted to the old code, start wrapping modules into classes. Replace global/shared variables with interfaces.
    • Now what you have will be a compiler in C++ (not quite though).
    • Draw a class diagram of all the classes in your system, and see how they are communicating.
    • Draw another one using the same classes and see how they ought to communicate.
    • Refactor the code to transform the first diagram to the second. (this might be messy and tricky)
    • Remember to use C++ code for all new code added.
    • If you have some time left, try replacing data structures one by one to use the more standardized STL or Boost.

提交回复
热议问题