在c++中,在哪些情况下使用结构体比使用类更好?
当前回答
什么时候你会选择使用struct 在c++中什么时候使用类?
我在定义函子和POD时使用struct。否则我就用class。
// '()' is public by default!
struct mycompare : public std::binary_function<int, int, bool>
{
bool operator()(int first, int second)
{ return first < second; }
};
class mycompare : public std::binary_function<int, int, bool>
{
public:
bool operator()(int first, int second)
{ return first < second; }
};
其他回答
它们是一样的,只是默认值不同(class默认为private, struct默认为public),所以理论上它们是完全可以互换的。
所以,如果我只是想打包一些信息来移动,我使用一个结构体,即使我在那里放了一些方法(但不是很多)。如果它是一个最不透明的东西,其中主要使用将通过方法,而不是直接到数据成员,我使用一个完整的类。
struct对我有帮助的一个地方是,当我有一个系统从另一个系统接收固定格式的消息(通过串行端口)时。您可以将字节流转换为定义字段的结构,然后轻松访问这些字段。
typedef struct
{
int messageId;
int messageCounter;
int messageData;
} tMessageType;
void processMessage(unsigned char *rawMessage)
{
tMessageType *messageFields = (tMessageType *)rawMessage;
printf("MessageId is %d\n", messageFields->messageId);
}
显然,这与您在C中所做的事情相同,但我发现必须将消息解码为类的开销通常是不值得的。
什么时候你会选择使用struct 在c++中什么时候使用类?
我在定义函子和POD时使用struct。否则我就用class。
// '()' is public by default!
struct mycompare : public std::binary_function<int, int, bool>
{
bool operator()(int first, int second)
{ return first < second; }
};
class mycompare : public std::binary_function<int, int, bool>
{
public:
bool operator()(int first, int second)
{ return first < second; }
};
摘自c++ FAQ Lite:
The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults. struct and class are otherwise functionally equivalent. OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.
在用我的主要语言c++编程多年之后,我得出了一个死结论,那就是这是c++的另一个愚蠢的特性。
两者之间没有真正的区别,我也没有理由花额外的时间来决定是应该将实体定义为结构体还是类。
要回答这个问题,请随时将实体定义为结构。默认情况下,成员将是公开的,这是规范。但更重要的是,默认情况下继承将是公开的。受保护继承和更糟糕的私有继承是例外。
我从来没有遇到过私人继承是正确做法的案例。是的,我试图发明问题来使用私有继承,但它不起作用。如果不使用访问器关键字,面向对象编程的角色模型Java默认为公共继承。顺便提一下,Java不允许在继承类上访问关键字,它们只能被公开继承。所以你可以看到,cpp团队在这里真的很失败。
另一件令人沮丧的事情是,如果你定义为类,声明为结构,你会得到编译警告。就好像这是影响程序性能或准确性的东西一样。一个回答还指出,MSVC可能会产生编译器错误。
Those persons that use classes when it is raining and structs when it is shining are doing so based on what they have been taught. It's not something they discovered to be true. Java does not have a pair of names for classes, and only have the class keyword. If you want a data structure, simply make all your members public and don't add functions. This works in Java and I don't see any problem. What's the problem? You need 4 or 5 characters of BOM code to determine how to interpret the context of a class entity.
推荐文章
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 公共朋友交换成员函数
- 如何在Go中使用c++
- 自定义c++分配器的引人注目的例子?
- RAII和c++中的智能指针
- 如何构建和使用谷歌TensorFlow c++ api
- 接口方法的最终参数-有什么意义?
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- 在Java中使用“final”修饰符
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- Std::cin输入空格?
- c++标准是否要求iostreams的性能很差,或者我只是在处理一个糟糕的实现?