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

后端 未结 9 979
执笔经年
执笔经年 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 15:06

    Yes. Struct can inherit from a class and vice versa. The accessibility rule is

    $11.2/2- "In the 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."

    EDIT 2: So you can change your class as:. Note that it is a bad idea to have public data members usually.

    class user_messages {  // i also changed the name when OP was modified :)
    public:   
       std::list messages;  
    };
    

提交回复
热议问题