为什么有头文件和.cpp文件? [英] Why have header files and .cpp files?

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

问题描述

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

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

推荐答案

好吧,主要原因是要分离接口从实施。头文件声明一个类(或正在执行的任何东西)将执行的操作,而cpp文件定义了其如何执行这些功能。

Well, the main reason would be for separating the interface from the implementation. The header declares "what" a class (or whatever is being implemented) will do, while the cpp file defines "how" it will perform those features.

因此,使用标头的代码不一定需要了解实现的所有细节以及仅为此所需的任何其他类/标头。

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.

这不是完美的,您通常会使用

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.

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

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