头文件中的类实现==样式错误? [英] Class implementation in a header file == bad style?

查看:78
本文介绍了头文件中的类实现==样式错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在C ++中,为什么会有头文件和cpp文件?

Possible Duplicate:
In C++ why have header files and cpp files?

我想知道是否建议在头文件中编写整个类c ++?并使用include包含该类,就像java进行oop编程一样.风格不好吗?谁能推荐我一个好的" C ++ oop样式指南?我也想知道模板编程到底是什么,当他们谈论具有C ++经验的人时.

I was wondering if it is advisable to write a the whole class c++ in a header file ? and use include to include the class, in a similar way that java does its oop programming. Is it a bad style ? can anyone recommend me a "good" C++ oop style guide ? Also I wondering if template programming is what they really mean when they talk about someone who is experienced in c++.

推荐答案

在头文件中包含函数定义会产生一些连锁反应,通常避免这样做.以这种方式定义的函数可能会内联,这将增加可执行文件的大小,从而影响性能(以正面或负面的方式).另外,这样做会增加构建时间,因为所有定义都符合要求会增加您必须包含其他文件等的可能性.

Including function definitions in a header file has some knock on effects and it's generally avoided. Functions defined in this manner are likely to be inlined, which will increase the size of your executable which can effect performance (in a positive or a negative way). Also, doing this can increase your build times, as having all definitions in line will increase the likelihood that you have to include additional files, etc.

一般做法是将类接口放在头文件(.h)中,并将其实现放在实现文件(.cpp)中.

The general practice is to put a classes interface in a header file (.h) and its implementation in a implementation file (.cpp).

在某些情况下,要求所有代码都可用-模板就是这种情况. STL广泛使用模板,并且必要时在头文件中包含函数的实现.

In some cases it's required to have all of the code available -- as is the case with templates. The STL makes extensive use of templates and implementations of functions are included in the header files by necessity.

这篇关于头文件中的类实现==样式错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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