In C ++, why is a structure actually a class?

another topic and the answers there made me ask this question:

Why does C ++ allow a struct to behave just like a class ? On the one hand, C ++ made it compatible with C-struct , making its members public by default (as in C), and on the other hand, it made it look like a class, allowing it to be inherited from classes and using other object- oriented methods (not as strong as C-struct ). Why didn't he just make it an old C-struct without OOP? Any special reason?

+6
c ++ oop struct class backwards-compatibility
source share
6 answers

In terms of language, structures and associations are just types of classes. This simplifies the language formulation if there are fewer concepts (with a small letter "c"), and also indicates that the language is less error prone, since it is less easy to skip something "obvious" if each common property should have been prescribed for each of the structures , unions and non-structural, non-unit classes.

C ++ classes have great potential functionality over C structures, but since C structures can be thought of as a degenerate C ++ class, it’s easiest to allow them to do just that. There is no use to the concept of a special structure, as well as the concept of a class.

From ISO / IEC 14882: 2003, 9 [class] / 4:

A structure is a class defined using the key of the struct class; its members and base classes are public by default. A union is a class defined using the key of the union class; its members are public by default and only contain one data item at a time.

+6
source share

This allows existing structures to be inserted into C ++ code in a more natural way. For example, you can add member functions to a structure and inherit from a structure, which would not be possible if structures and classes lived in different universes.

Straustrup's original intention was to avoid breaking the community between the traditional "structural" C-style camp and the "class" group. He also cited the benefits of having only one concept instead of two.

+7
source share
  • "public" default for structure fields is required for compatibility with C code that can access structure fields.
  • "public" default for structure inheritance requires that the child class can be used instead of the base structure.
  • You can create a structure in a different data type than classes (that is, prohibit types and methods of accessing them), but this would be inconvenient for programming and adds a lot of unnecessary work for the compiler to the developers.
+2
source share

In C, there were only initial structures. Orientation of objects began when libraries were developed, when pointers to these structures were transferred to a set of library functions that depended on this structure. A good example is the Win32 API. This is not a C ++ interface; it is a C-interface; but it is still object oriented.

Classes are almost the same as memory structures. Member functions are not stored as part of class member data. It is just a structure with extra function pointers at the end. Thus, the class function table is dereferenced in the same way that the Windows API uses object orientation, but it encapsulates it so that you do not see it.

Inheritance, polymorphism; what's the difference? People were fine with C, and still do fine with C.

+1
source share

By allowing what you declare, since struct is indeed a class, it provides type safety when creating the C interface.

You can directly declare your structure for your C interface:

 struct Foo; 

You can declare methods on it

 void doStuffThatModifiesFoo( struct Foo * foo, ... ); struct Bar getStuffFromFoo( const struct Foo * foo ); 

You can also write creation and destruction methods for it.

Below you do not implement Foo as a C structure, but as a class, but your C clients do not need to know this. This is better than passing it as a void *, then throwing it (it is unsafe if someone passes you a void * of a completely different type and you throw it).

+1
source share

The "C compatibility" problem is intended in one direction only: the old, valid C code must also compile as C ++ code. Another way is not possible as soon as any function of the language used only by C ++ is used.

This means that in C ++ you always write classes, you can not use the struct keyword at all; although some, including myself, find it useful to show that a class is just a simple set of named values ​​without real encapsulation or possibly complex behavior.

0
source share

All Articles