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

后端 未结 6 603
谎友^
谎友^ 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:18

    In C there were only structs to begin with. Object orientation began when libraries were designed when pointers to those structures were passed to a set of library functions that were dependent on that structure. One good example is the Win32 API. It isn't a C++ interface, it's a C interface; but it's still object oriented.

    Classes are almost the same as structures memory-wise. The member functions are not stored as part of the class member data. It's simply a structure with extra function pointers on the end. So a class' function table is dereferenced in the same way that Windows API uses object orientation, but it encapsulates it so you don't see it.

    Inheritance, polymorphism; who cares? People were fine with C, and still doing fine with C.

提交回复
热议问题