Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361802

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?

Upvotes: 6

Views: 1448

Answers (6)

Mephane
Mephane

Reputation: 2022

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.

Upvotes: 0

Marcelo Cantos
Marcelo Cantos

Reputation: 186118

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.

Upvotes: 7

CB Bailey
CB Bailey

Reputation: 793269

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.

Upvotes: 6

Shelwien
Shelwien

Reputation: 2200

  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.

Upvotes: 2

CashCow
CashCow

Reputation: 31455

Allowing something you declare as struct to really be a class allows type-safety when creating a C interface.

You can forwardly declare your struct 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 create and destroy methods for it.

Underneath you implement Foo not as a C struct but as a class but your C clients do not need to know that. This is better than passing it around as a void * then casting (not safe if someone passes you a void* to a completely different type and you cast it).

Upvotes: 1

oldSkool
oldSkool

Reputation: 1232

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.

Upvotes: 1

Related Questions