After choosing a few questions:
"What is the correct way to have C and C ++ code in the same .cpp file?" "Can I continue to use g ++ to compile my hybrid code?"
If you want to mix C ++ C-style in the same file as regular C ++, just go ahead and do it. You can trust the compiler to select any problems - they will be minimal and will not affect the structure. According to him, you are not interested in getting a C-linkage for yourself, therefore, even if the C-code is in your own file, compile it as C ++. In fact, this is often done as a way to migrate from C to C ++.
If you take this approach, your code will not be truly hybrid C / C ++. This is C ++ with some code using C-style procedural idioms. C ++ is fully intended to support this.
"Will professional programmers laugh at me if I continue to naively embed C material in my C ++ code?"
It depends on where you use it and why. Well-structured C code is good code. Sometimes C + is much better than C, with special problems. Think Before You Use C-Style Dynamic Memory Management. You deserve to be laughed at if you use raw malloc()/free() and make a mistake.
I suggest that if you start a similar approach, you can take the time to look back and think it would be better to use C ++ idioms for procedural C.
source share