Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 it compatible with C-struct by making it's members public by default (just like in C), while on the other hand, it made it look-like class by allowing it to be inherited from classes, and applying other object-oriented techniques (not so much like C-struct anymore). Why did it not make it just plain old C-struct with no OOP? Any special reason?

like image 494
Nawaz Avatar asked Dec 13 '10 10:12

Nawaz


People also ask

Is struct in C same as class?

In C++, structs and classes are pretty much the same; the only difference is that where access modifiers (for member variables, methods, and base classes) in classes default to private, access modifiers in structs default to public.

Is a struct the same as a class?

Classes and Structs (C++)The two constructs are identical in C++ except that in structs the default accessibility is public, whereas in classes the default is private. Classes and structs are the constructs whereby you define your own types.

Why do we need class if we have struct?

Using a struct we can achieve all the functionality of a class : constructors (that can be modified/overloaded), destructors (that can be modified/overloaded), operator overloading, instance methods, static methods, public / private / protected fields/methods.

Is a struct basically a class?

So what is the difference? Besides the word struct vs class, there is only one difference. In a struct, members default to public. In a class, members default to private.


2 Answers

It allows existing structs to be fitted in with C++ code in a more natural way. For instance, you can add member functions to a struct and inherit from a struct, which wouldn't be possible if structs and classes inhabited different universes.

Stroustrup's original intent was to avoid a fracturing of the community between the traditional C-style "struct" camp, and the OO "class" crowd. He also cited the benefits of having just one concept instead of two.

like image 188
Marcelo Cantos Avatar answered Sep 29 '22 04:09

Marcelo Cantos


From a language point of view, structures and unions are just types of class. It makes the language specification simpler if there are fewer concepts (with a small letter 'c') and it also makes specifying the language less error prone as it is less easy to miss something 'obvious' out if every common property had to spelled out for each of structures, unions and non-structure, non-union classes.

C++ classes have a lot of potential functionality over C structures but as C structures can be viewed as a degenerate C++ class, it is simplest to allow them to be exactly this. There is no benefit to having a special structure concept as well as a class concept.

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

A structure is a class defined with the class-key struct; its members and base classes are public by default. A union is a class defined with the class-key union; its members are public by default and it holds only one data member at a time.

like image 21
CB Bailey Avatar answered Sep 29 '22 03:09

CB Bailey