在c++中,在哪些情况下使用结构体比使用类更好?


当前回答

从技术上讲,这两者在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 -喜欢。

其他回答

在用我的主要语言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.

正如每个人所说,唯一真正的区别是默认访问。但是,当我不想对简单的数据类进行任何形式的封装时,即使实现了一些helper方法,我也会特别使用struct。例如,当我需要这样的东西时:

struct myvec {
    int x;
    int y;
    int z;

    int length() {return x+y+z;}
};

struct和class在本质上是相同的,尽管在可见性方面有不同的默认值,struct的默认值是公共的,而类的默认值是私有的。您可以通过适当地使用private和public将其中一个更改为另一个。它们都允许继承、方法、构造函数、析构函数以及面向对象语言的所有其他优点。

然而,两者之间的一个巨大区别是C支持struct作为关键字,而class不支持。这意味着可以在包含文件中使用一个可以#include到c++或C中的结构体,只要该结构体是一个普通的C风格结构体,并且包含文件中的其他内容与C兼容,即没有c++特定的关键字,如private, public, no方法,no继承,等等等等。

C风格结构体可以与其他支持使用C风格结构体在接口上来回传输数据的接口一起使用。

C风格结构是一种模板(不是c++模板,而是一种模式或模板),用于描述内存区域的布局。多年来,C语言和C插件(这里是Java、Python和Visual Basic)已经创建了可用的接口,其中一些与C风格的结构体一起工作。

从技术上讲,这两者在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 -喜欢。

类。

默认情况下,类成员是私有的。

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表示数据结构,其中成员可以取任何值 那样容易些。