In C++, why struct is in fact class?

后端 未结 6 649
谎友^
谎友^ 2021-01-05 11:13

The other topic and responses there made me ask this question:

Why does C++ allow struct to behave just like class? At one hand, C++ made

6条回答
  •  春和景丽
    2021-01-05 11:40

    1. "public" default for struct fields is required for compatibility with C code which may access struct fields.
    2. "public" default for struct inheritance is required so that the child class could be used in place of base struct.
    3. It could be possible to make struct into a different data type than classes (ie disallow access type specifiers and methods in it), but that would be both inconvenient for programming and add a lot of unnecessary work for compiler developers.

提交回复
热议问题