在c++中,在哪些情况下使用结构体比使用类更好?
当前回答
只是从c++ 20 standard的角度(从N4860工作)来解决这个问题…
类是一种类型。关键字“class”和“struct”(以及“union”)在c++语法中是“class-key”,选择class或struct的唯一功能意义是:
类键决定是否…默认情况下访问是public或private(11.9)。
数据成员默认可访问性
class关键字的结果是private-by-default成员,而' struct关键字的结果是public-by-default成员,在11.9.1的例子中有说明:
类X { int;// X::a默认为private:使用的类
对…
struct S { int;// S::a默认为public: struct被使用
基类默认可访问性
1.9还说:
在基类没有访问说明符的情况下,当派生类使用类键结构体定义时假定为public,当类使用类键类定义时假定为private。
需要一致使用结构体或类的情况……
有一个要求:
在类模板的重声明、部分特化、显式特化或显式实例化中,类键应与原始类模板声明一致(9.2.8.3)。
...在任何详细类型说明符中,枚举关键字应使用指向枚举(9.7.1),联合类键应使用指向联合(11.5),类或结构的类键必须是 用于指非并体类(11.1)。
以下是不需要一致性的例子:
struct S {} S; 类S* p = &s;/ /好吧
不过,一些编译器可能会对此提出警告。
有趣的是,虽然你用struct、class和union创建的类型都被称为“类”,但我们有…
标准布局结构体是用类键结构体或类键类定义的标准布局类。
...所以在标准语中,当谈到标准布局结构体时,它使用“struct”来暗示“不是联合”。
我很好奇在其他术语中是否也有类似的“struct”用法,但要对标准进行详尽的搜索,工作量太大了。欢迎对此发表评论。
其他回答
如果你写的库内部是c++,但API可以被C或c++代码调用,你可以在c++中使用"struct"。你只需要创建一个包含struct和全局API函数的头文件,就可以向C和c++代码公开,如下所示:
// C access Header to a C++ library
#ifdef __cpp
extern "C" {
#endif
// Put your C struct's here
struct foo
{
...
};
// NOTE: the typedef is used because C does not automatically generate
// a typedef with the same name as a struct like C++.
typedef struct foo foo;
// Put your C API functions here
void bar(foo *fun);
#ifdef __cpp
}
#endif
然后,您可以使用c++代码在c++文件中编写函数栏(),并使其可从C调用,两个世界可以通过声明的结构共享数据。当然,在混合使用C和c++时还有其他注意事项,但这是一个简化的示例。
类。
默认情况下,类成员是私有的。
class test_one {
int main_one();
};
等于
class test_one {
private:
int main_one();
};
所以如果你尝试
int two = one.main_one();
我们将得到一个错误:main_one是私有的,因为它不可访问。我们可以 通过指定它的公共ie来初始化它来解决它
class test_one {
public:
int main_one();
};
结构体。
struct是一个类,其成员默认为public。
struct test_one {
int main_one;
};
意味着main_one是私有的,即
class test_one {
public:
int main_one;
};
我用struct表示数据结构,其中成员可以取任何值 那样容易些。
我只在需要保存一些没有任何成员函数与之关联的数据(对成员数据进行操作)并直接访问数据变量时使用struct。
从文件和套接字流等读取/写入数据。在函数参数太多且函数语法看起来太冗长的结构中传递函数参数。
从技术上讲,类和结构之间没有太大的区别,除了默认的可访问性。 更重要的是,它取决于你如何使用它的编程风格。
在用我的主要语言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.
从技术上讲,这两者在c++中是相同的——例如,结构体可能具有重载操作符等。
然而:
当我希望同时传递多种类型的信息时,我使用结构体 当我处理一个“功能性”对象时,我使用类。
希望能有所帮助。
#include <string>
#include <map>
using namespace std;
struct student
{
int age;
string name;
map<string, int> grades
};
class ClassRoom
{
typedef map<string, student> student_map;
public :
student getStudentByName(string name) const
{ student_map::const_iterator m_it = students.find(name); return m_it->second; }
private :
student_map students;
};
例如,我在这里的get…()方法中返回一个struct student -喜欢。