Using/Mixing C in C++ code?

前端 未结 12 1589
南旧
南旧 2020-12-24 03:03

Is using C in C++ bad?

Many people have told me that using C in C++ is bad because it\'s not as safe, and it requires more memory management. I keep telling them tha

12条回答
  •  我在风中等你
    2020-12-24 03:22

    Well, I'm in a strange situation. I am working on a system that is supposed to be in C++ (C++ compiler is used and term C++ is used), but everything is written in C. This is very frustrating, because it is getting to a point where I have to 'prove' C++ is better to use than C, even though we are coding in C++. It was all hell when I introduced std::string. My take on it is that everything now is starting to get cluttered (mixing of C and C++). There are rare instances of error handling. In fact, I think I can count 3 system wide try-catch statements. The code is messy, memory leaks are prominent and finding an error is a needle in a haystack. There are hundreds of lines of code that can be replaced by C++ functions. I'd say writing the code is more efficient, cleaner and easier to understand.

    From my experience, yes of course you can mix C and C++. You can pretty much do whatever you want, but maintaining, debugging and actually figuring out what's going on becomes a problem. It's easy to say you're going to clean-up your memory allocations, but maybe someone else uses your code and doesn't. Maybe you forget to do it and you waste hours on finding silly bugs, instead of using that time to do something productive. I don't even think there should be an argument. When you do C++, do C++. When you do C, do C.

提交回复
热议问题