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

后端 未结 25 1691
误落风尘
误落风尘 2020-11-22 00:18

In what scenarios is it better to use a struct vs a class in C++?

相关标签:
25条回答
  • 2020-11-22 00:50

    Structs by default have public access and classes by default have private access.

    Personally I use structs for Data Transfer Objects or as Value Objects. When used as such I declare all members as const to prevent modification by other code.

    0 讨论(0)
  • 2020-11-22 00:50

    I use struct only when I need to hold some data without any member functions associated to it (to operate on the member data) and to access the data variables directly.

    Eg: Reading/Writing data from files and socket streams etc. Passing function arguments in a structure where the function arguments are too many and function syntax looks too lengthy.

    Technically there is no big difference between class and struture except default accessibility. More over it depends on programming style how you use it.

    0 讨论(0)
  • 2020-11-22 00:53

    Class.

    Class members are private by default.

    class test_one {
        int main_one();
    };
    

    Is equivalent to

    class test_one {
      private:
        int main_one();
    };
    

    So if you try

    int two = one.main_one();
    

    We will get an error: main_one is private because its not accessible. We can solve it by initializing it by specifying its a public ie

    class test_one {
      public:
        int main_one();
    };
    

    Struct.

    A struct is a class where members are public by default.

    struct test_one {
        int main_one;
    };
    

    Means main_one is private ie

    class test_one {
      public:
        int main_one;
    };
    

    I use structs for data structures where the members can take any value, it's easier that way.

    0 讨论(0)
  • 2020-11-22 00:54

    As every one says, the only real difference is the default access. But I particularly use struct when I don't want any sort of encapsulation with a simple data class, even if I implement some helper methods. For instance, when I need something like this:

    struct myvec {
        int x;
        int y;
        int z;
    
        int length() {return x+y+z;}
    };
    
    0 讨论(0)
  • 2020-11-22 00:58

    From the 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.

    0 讨论(0)
  • 2020-11-22 01:00

    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 this breaks Visual Studio builds due to non-conformance, so that compiler will emit a warning when you do this.)

    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.

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