C++: Can a struct inherit from a class?

后端 未结 9 956
执笔经年
执笔经年 2020-12-07 14:39

I am looking at the implementation of an API that I am using.

I noticed that a struct is inheriting from a class and I paused to ponder on it...

First, I d

相关标签:
9条回答
  • 2020-12-07 14:40

    The main thing to understand is that structs come from C, whereas classes are C++. This means that although structs ARE first-class object-orientated citizens, they also have a legacy purpose, which is the reason for classes being separate and structs being default-access public. However, once this is done with, they're absolutely and totally identical and interchangable in every way.

    0 讨论(0)
  • 2020-12-07 14:42

    The only difference between a struct and a class is the default access level for members (private for classes, public for structs). This means that a struct should be able to inherit from a class, and vice-versa.

    However, there is usually a difference in how structs and classes are used that is not mandated by the standard. structs are often used for pure data, ( or objects without polymorphism depending on your projects preference) and classes are used for the other cases. I emphasise that this is just a stylistic difference and not required.

    0 讨论(0)
  • 2020-12-07 14:45

    A struct is the same thing as a class except that a class defaults its members to private while a struct defaults its members to public. As a result, yes, you can inherit between the two. See in C++, can I derive a class from a struct.

    0 讨论(0)
  • 2020-12-07 14:52

    struct and class are pretty much interchangeable - just with different defaults in that classes default to private inheritance and members, structs to public. The class keyword (and not struct) must be used for eg. "template <class T>".

    That said, many programmers use the two to give a slight suggestion to a programmer reading the code: by using a struct you're subtly suggesting a less encapsulating, OO design. A struct might be used internal to a library - where getting at the guts of it all is fair game, whereas classes are used on the boundary where API changes would inconvenience clients and better abstraction is useful. This very loose convention has grown out of the difference in default accessibility - lazy/efficient/concise (take your pick) programmers do what's easiest unless there's a benefit otherwise, and not typing access specifiers is nice when possible.

    0 讨论(0)
  • 2020-12-07 15:03

    Yes a struct can inherit from a class. struct and class differ only in the access-specifier assumed for the members and for a base classes (or structs) if not specified explicitly in C++ . For structs it's public. For classes it's private.

    The sentence you quote from the manual is about the concept of a class in C++, as compared to the concept of a data structure in C. In C++ new keyword - class was introduced to better reflect the change in the concept, but for compatibility with code in C, an old keyword struct was left and it's meaning is as described above.

    0 讨论(0)
  • 2020-12-07 15:05

    Yes, struct can inherit from class in C++.

    In C++, classes and struct are the same except for their default behaviour with regards to inheritance and access levels of members.

    C++ class

    • Default Inheritance = private
    • Default Access Level for Member Variables and Functions = private

    C++ struct

    • Default Inheritance = public
    • Default Access Level for Member Variables and Functions = public
    0 讨论(0)
提交回复
热议问题