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

后端 未结 6 661
谎友^
谎友^ 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条回答
  •  Happy的楠姐
    2021-01-05 11:18

    The "compatibility with C" issue is only meant in one direction: Old, valid C-code should also compile as C++ code. The other way round is impossible as soon as any language feature that only C++ has is being used.

    That means in C++, you always write classes, you can omit using the keyword struct at all ; though some, including me, think they come in handy to show that a class just a simple collection of named values, with no real encapsulation or possibly complex behaviour.

提交回复
热议问题