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

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

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

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

    Both struct and class are the same under the hood though with different defaults as to visibility, struct default is public and class default is private. You can change either one to be the other with the appropriate use of private and public. They both allow inheritance, methods, constructors, destructors, and all the rest of the goodies of an object oriented language.

    However one huge difference between the two is that struct as a keyword is supported in C whereas class is not. This means that one can use a struct in an include file that can be #include into either C++ or C so long as the struct is a plain C style struct and everything else in the include file is compatible with C, i.e. no C++ specific keywords such as private, public, no methods, no inheritance, etc. etc. etc.

    A C style struct can be used with other interfaces which support using C style struct to carry data back and forth over the interface.

    A C style struct is a kind of template (not a C++ template but rather a pattern or stencil) that describes the layout of a memory area. Over the years interfaces usable from C and with C plug-ins (here's looking at you Java and Python and Visual Basic) have been created some of which work with C style struct.

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

    When would you choose to use struct and when to use class in C++?

    I use struct when I define functors and POD. Otherwise I use class.

    // '()' is public by default!
    struct mycompare : public std::binary_function<int, int, bool>
    {
        bool operator()(int first, int second)
        { return first < second; }
    };
    
    class mycompare : public std::binary_function<int, int, bool>
    {
    public:
        bool operator()(int first, int second)
        { return first < second; }
    };
    
    0 讨论(0)
  • 2020-11-22 01:03

    All class members are private by default and all struct members are public by default. Class has default private bases and Struct has default public bases. Struct in case of C cannot have member functions where as in case of C++ we can have member functions being added to the struct. Other than these differences, I don't find anything surprising about them.

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

    Structs (PODs, more generally) are handy when you're providing a C-compatible interface with a C++ implementation, since they're portable across language borders and linker formats.

    If that's not a concern to you, then I suppose the use of the "struct" instead of "class" is a good communicator of intent (as @ZeroSignal said above). Structs also have more predictable copying semantics, so they're useful for data you intend to write to external media or send across the wire.

    Structs are also handy for various metaprogramming tasks, like traits templates that just expose a bunch of dependent typedefs:

    template <typename T> struct type_traits {
      typedef T type;
      typedef T::iterator_type iterator_type;
      ...
    };
    

    ...But that's really just taking advantage of struct's default protection level being public...

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

    An advantage of struct over class is that it save one line of code, if adhering to "first public members, then private". In this light, I find the keyword class useless.

    Here is another reason for using only struct and never class. Some code style guidelines for C++ suggest using small letters for function macros, the rationale being that when the macro is converted to an inline function, the name shouldn't need to be changed. Same here. You have your nice C-style struct and one day, you find out you need to add a constructor, or some convenience method. Do you change it to a class? Everywhere?

    Distinguishing between structs and classes is just too much hassle, getting into the way of doing what we should be doing - programming. Like so many of C++'s problems, it arises out of the strong desire for backwards compatability.

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

    To answer my own question (shamelessly), As already mentioned, access privileges are the only difference between them in C++.

    I tend to use a struct for data-storage only. I'll allow it to get a few helper functions if it makes working with the data easier. However as soon as the data requires flow control (i.e. getters/setters that maintain or protect an internal state) or starts acquring any major functionality (basically more object-like), it will get 'upgraded' to a class to better communicate intent.

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