使用/ C ++中code混合使用C? [英] Using/Mixing C in C++ code?

查看:112
本文介绍了使用/ C ++中code混合使用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时当使用C C ++不好?

很多人都告诉我,在C ++中使用C是不好的,因为它不是安全的,它需要更多的内存管理。我一直告诉他们,只要你知道你做什么,你删除你的新的和免费的的malloc的那么C是没有问题的。

我目前在一个论坛上STD参数:: string的与一个char *正在发生。有些人说,分配一个简单的char *内存块更有效率,只要你释放它,它的罚款。在另一方面,我们有人们说的std :: string是优越的,因为它不涉及内存管理,但效率不高。

因此​​,这里的主要问题是:
在混合C / C ++不好?如果您的只使用100%的C ++,当你编码C ++?

任何答案将是AP preciated!


解决方案

  

我一直告诉他们,只要你知道你做什么,你删除你的新的和免费的的malloc的那么C是没有问题的。


这是真实的;如果你是格外小心,并确保你手动清理的东西了,那么它是没有问题的。但你真的有时间做到这一点?到每次调用新的可以抛出则为std :: bad_alloc 。你的总是的捕获的每个的异常,可以抛出手动清理任何资源?

我斗胆猜测的答案是不,因为它是非常繁琐的编写code这样的,这是很难做到绝对保证这样写的code是正确的,甚至在稀有故障的情况下

如果答案是是,那么你为什么浪费了这么多时间来担心资源管理? C ++成语像范围限制的资源管理(SBRM;通常被称为资源获取就是初始化(RAII))等的标准模板库库是有帮助你更轻松地编写正确的code。为什么事情硬的方式,当你不就得了?


  

如果你只使用100%的C ++,当你编码C ++?


是的,但如果有一个C库,做你需要的东西,或者如果你有,你要使用传统的C code,你当然可以使用code;只是一定要小心。常与C code到互操作最彻底的方法是写它周围的一个C ++包装。

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 that as long as you know what your doing, and you delete your new's and free your malloc's then C isn't a problem.

I'm currently on a forum where an argument over std::string vs. a char* is taking place. Some people are saying that allocating a simple char* memory block is more efficient, and as long as you deallocate it, it's fine. On the other hand we have people saying that std::string is superior because it has no memory management involved but is less efficient.

So the main question here is: Is mixing C/C++ bad? Should your ONLY use 100% C++ when your coding C++?

Any answers would be appreciated!

解决方案

I keep telling them that as long as you know what your doing, and you delete your new's and free your malloc's then C isn't a problem.

This is true; if you are extraordinarily careful and ensure that you manually clean things up, then it isn't a problem. But do you really have the time to do that? Every call to new can throw std::bad_alloc. Do you always catch every exception that can be thrown and manually clean up any resources?

I'd hazard to guess the answer to that is "no," because it is very tedious to write code like that and it is difficult to be absolutely sure that code written like that is correct, even in the case of rare failures.

If the answer is "yes," then why are you wasting so much time worrying about resource management? C++ idioms like scope-bound resource management (SBRM; more commonly known as resource acquisition is initialization (RAII)) and libraries like the standard template library are there to help you write correct code more easily. Why do things the hard way when you don't have to?

Should you ONLY use 100% C++ when your coding C++?

Yes, though if there is a C library that does something you need, or if you have legacy C code that you want to use, you can certainly use that code; just be sure to be careful. Often the cleanest way to interop with C code is to write a C++ wrapper around it.

这篇关于使用/ C ++中code混合使用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆