When should you use a class vs a struct in C++?

在哪种情况下最好在C ++中使用structclass


Differences between a class and a struct in C++ are that structs have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance and can have member functions.

I would recommend using structs as plain-old-data structures without any class-like features, and using classes as aggregate data structures with private data and member functions.


As everyone else notes there are really only two actual language differences:

  • struct defaults to public access and class defaults to private access.
  • When inheriting, struct defaults to public inheritance and class defaults to private inheritance. (Ironically, as with so many things in C++, the default is backwards: public inheritance is by far the more common choice, but people rarely declare struct s just to save on typing the " public " keyword.
  • But the real difference in practice is between a class / struct that declares a constructor/destructor and one that doesn't. There are certain guarantees to a "plain-old-data" POD type, that no longer apply once you take over the class's construction. To keep this distinction clear, many people deliberately only use struct s for POD types, and, if they are going to add any methods at all, use class es. The difference between the two fragments below is otherwise meaningless:

    class X
    {
      public:
    
      // ...
    };
    
    struct X
    {
      // ...
    };
    

    (Incidentally, here's a thread with some good explanations about what "POD type" actually means: What are POD types in C++?)


    There are lots of misconceptions in the existing answers.

    Both class and struct declare a class.

    Yes, you may have to rearrange your access modifying keywords inside the class definition, depending on which keyword you used to declare the class.

    But, beyond syntax, the only reason to choose one over the other is convention/style/preference.

    Some people like to stick with the struct keyword for classes without member functions, because the resulting definition "looks like" a simple structure from C.

    Similarly, some people like to use the class keyword for classes with member functions and private data, because it says "class" on it and therefore looks like examples from their favourite book on object-oriented programming.

    The reality is that this completely up to you and your team, and it'll make literally no difference whatsoever to your program.

    The following two classes are absolutely equivalent in every way except their name:

    struct Foo
    {
       int x;
    };
    
    class Bar
    {
    public:
       int x;
    };
    

    You can even switch keywords when redeclaring:

    class Foo;
    struct Bar;
    

    (although some compilers will emit a warning when you do this, on the assumption that you probably didn't intend to do something so confusing and that you should therefore be prompted to double-check your code.)

    and the following expressions both evaluate to true:

    std::is_class<Foo>::value
    std::is_class<Bar>::value
    

    Do note, though, that you can't switch the keywords when redefining; this is only because (per the one-definition rule) duplicate class definitions across translation units must "consist of the same sequence of tokens". This means you can't even exchange const int member; with int const member; , and has nothing to do with the semantics of class or struct .

    链接地址: http://www.djcxy.com/p/28056.html

    上一篇: 为什么可变结构是“邪恶的”?

    下一篇: 什么时候应该在C ++中使用类和结构?