Class vs Struct for data only?

后端 未结 7 639
暖寄归人
暖寄归人 2020-11-28 15:35

Is there any advantage over using a class over a struct in cases such as these? (note: it will only hold variables, there will never be functions)

class Foo         


        
相关标签:
7条回答
  • 2020-11-28 16:15

    One side point is that structs are often used for aggregate initialized data structures, since all non-static data members must be public anyway (C++03, 8.5.1/1).

    struct A {  // (valid)
    {
       int a;
       int b;
    } x = { 1, 2 };
    
    struct A {  // (invalid)
    private:
       int a;
       int b;
    } x = { 1, 2 };
    
    class A {  // (invalid)
       int a;
       int b;
    } x = { 1, 2 };
    
    class A {  // (valid)
    public:
       int a;
       int b;
    } x = { 1, 2 };
    
    class A {  // (invalid)
    public:
       int a;
    private:
       int b;
    } x = { 1, 2 };
    
    0 讨论(0)
  • 2020-11-28 16:23

    There is no real advantage of using one over the other, in c++, the only difference between a struct and a class is the default visibility of it's members (structs default to public, classes default to private).

    Personally, I tend to prefer structs for POD types and use classes for everything else.

    EDIT: litb made a good point in the comment so I'm going to quote him here:

    one important other difference is that structs derive from other classes/struct public by default, while classes derive privately by default.

    0 讨论(0)
  • 2020-11-28 16:31

    The only difference between a class and a struct is that struct members are public by default and class members are private by default. So I say go with whichever one you like best. I'm sure there are arguments to be made in terms of which one is more readable, but I really don't think it's a big deal.

    0 讨论(0)
  • 2020-11-28 16:32

    "(note: it will only hold variables, there will never be functions)"

    Never is a big word. Usually "never" means "eventually". Since that's the case, I'd suggest you use a class. That way, when things change, you don't have so much to change.

    The Java (and Python) folks have gotten along fine with everything being a class. It hasn't hurt them any to not have these specialized method-less classes that C++ calls a "struct".

    0 讨论(0)
  • 2020-11-28 16:37

    Essentially the choice between a struct and a class comes down to your style and how much you want to type.

    • If you only have public members in a class/struct you might as well use the struct keyword. It will save you having to type out "public:" later on.
    • The other reason to choose a struct over a class would be to implicitly document the intent of the object. So you would make POD types structs (even if they contain a constructors and some static helper methods etc), and you would use class for all the other "regular" classes.
    0 讨论(0)
  • 2020-11-28 16:39

    struct and class mean exactly the same thing in C++ with the exception that the default access for struct members and bases is public whereas it is private for classes. I tend to chose struct for classes that only have public members and classes for everything else, but it's only a style issue.

    0 讨论(0)
提交回复
热议问题