为什么在C ++中有头文件和.cpp文件? [英] Why have header files and .cpp files in C++?

查看:144
本文介绍了为什么在C ++中有头文件和.cpp文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C ++有头文件和.cpp文件?

Why does C++ have header files and .cpp files?

推荐答案

从实现。

这样可以减少依赖性所以使用头的代码不一定需要知道实现的所有细节和任何其他只需要的类/头。这将减少编译时间以及在实现中的某些内容发生变化时需要重新编译的数量。

This reduces dependencies so that code that uses the header doesn't necessarily need to know all the details of the implementation and any other classes/headers needed only for that. This will reduce compilation times and also the amount of recompilation needed when something in the implementation changes.

这不是完美的,你通常会使用像 Pimpl Idiom 正确分离接口和实现,但这是一个好的开始。

It's not perfect, and you would usually resort to techniques like the Pimpl Idiom to properly separate interface and implementation, but it's a good start.

这篇关于为什么在C ++中有头文件和.cpp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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