What are the differences between struct and class in C++?

后端 未结 30 3192
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:38

This question was already asked in the context of C#/.Net.

Now I\'d like to learn the differences between a struct and a class in C++. Please discuss the technical d

相关标签:
30条回答
  • 2020-11-21 06:01

    You forget the tricky 2nd difference between classes and structs.

    Quoth the standard (§11.2.2 in C++98 through C++11):

    In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class.

    And just for completeness' sake, the more widely known difference between class and struct is defined in (11.2):

    Member of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.

    Additional difference: the keyword class can be used to declare template parameters, while the struct keyword cannot be so used.

    0 讨论(0)
  • 2020-11-21 06:02

    STRUCT is a type of Abstract Data Type that divides up a given chunk of memory according to the structure specification. Structs are particularly useful in file serialization/deserialization as the structure can often be written to the file verbatim. (i.e. Obtain a pointer to the struct, use the SIZE macro to compute the number of bytes to copy, then move the data in or out of the struct.)

    Classes are a different type of abstract data type that attempt to ensure information hiding. Internally, there can be a variety of machinations, methods, temp variables, state variables. etc. that are all used to present a consistent API to any code which wishes to use the class.

    In effect, structs are about data, classes are about code.

    However, you do need to understand that these are merely abstractions. It's perfectly possible to create structs that look a lot like classes and classes that look a lot like structs. In fact, the earliest C++ compilers were merely pre-compilers that translates C++ code to C. Thus these abstractions are a benefit to logical thinking, not necessarily an asset to the computer itself.

    Beyond the fact that each is a different type of abstraction, Classes provide solutions to the C code naming puzzle. Since you can't have more than one function exposed with the same name, developers used to follow a pattern of _(). e.g. mathlibextreme_max(). By grouping APIs into classes, similar functions (here we call them "methods") can be grouped together and protected from the naming of methods in other classes. This allows the programmer to organize his code better and increase code reuse. In theory, at least.

    0 讨论(0)
  • 2020-11-21 06:05

    Quoting The C++ FAQ,

    [7.8] What's the difference between the keywords struct and class?

    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-21 06:05

    The difference between class and struct is a difference between keywords, not between data types. This two

    struct foo : foo_base { int x;};
    class bar : bar_base { int x; };
    

    both define a class type. The difference of the keywords in this context is the different default access:

    • foo::x is public and foo_base is inherited publicly
    • bar::x is private and bar_base is inherited privately
    0 讨论(0)
  • 2020-11-21 06:06

    Another main difference is when it comes to Templates. As far as I know, you may use a class when you define a template but NOT a struct.

    template<class T> // OK
    template<struct T> // ERROR, struct not allowed here
    
    0 讨论(0)
  • 2020-11-21 06:07

    The main difference between structure and class keyword in oops is that, no public and private member declaration present in structure.and the data member and member function can be defined as public, private as well as protected.

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