C ++:什么时候应该使用结构体而不是类,速度差异在哪里? [英] C++: When should I use structs instead of classes and where are the speed differences?
问题描述
-
何时应使用
struct
而不是类?我目前使用的类从OpenGL纹理包装器到位图字体。
When should I use a
struct
instead of a class? I'm currently using classes for everything from OpenGL texture wrappers to bitmap fonts.
是一个类,我使用就像一个 struct
Is a class that I use just like a struct
(no making usage of inheritance, polymorphism, etc.) still slower than a struct
?
推荐答案
您可能知道C ++中的结构和类别
Structs and classes in C++ as you may know differ solely by their default access level (and default accessibility of their bases: public for struct, private for class).
一些开发人员,包括我自己更喜欢使用 POD类型,即使用C样式结构,没有虚函数,基数等。结构体不应该有行为 - 它们只是数据放入一个对象的集合。
Some developers, including myself prefer to use structs for POD-types, that is, with C-style structs, with no virtual functions, bases etc. Structs should not have behavior - they are just a comglomerate of data put into one object.
但这当然是风格的问题,显然既不慢
But that is naturally a matter of style, and obviously neither is slower
这篇关于C ++:什么时候应该使用结构体而不是类,速度差异在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!