-4

From a design perspective, one could have added the class keyword to C++ to realize the desired OO principles like encapsulation, inheritance, polymorphism as well as constructors and destructors etc ... but at the same time leave the keyword struct just like it is defined in C and thus be able to a) compile legacy C code and b) define simple PODs.

I cannot see the reason for making a struct behave (almost) like a class in C++.

Or in other words: Why didn't they just leave the C struct keyword alone in C++?

Any hints ?

Angle.Bracket
  • 1,438
  • 13
  • 29
  • 5
    Backwards compatibility. – DeiDei May 08 '18 at 14:10
  • 2
    @DeiDei it would have been backwards compatible if it had been just a C ' `struct` – Angle.Bracket May 08 '18 at 14:13
  • @Ron with the exception of all struct members being public by default, hence 'almost' – Angle.Bracket May 08 '18 at 14:13
  • @Angle.Bracket The C++ struct is a superset of the C struct. – DeiDei May 08 '18 at 14:14
  • 1
    @DeiDei I know. But why is that so ? – Angle.Bracket May 08 '18 at 14:15
  • 2
    Imagine if C++ didn't have compatibility for C structs. `windows.h` would have to be rewritten for C++... – DeiDei May 08 '18 at 14:16
  • 1
    It seems you have a fundamental misunderstanding of how `struct` and standard layout types work in C++. – Mgetz May 08 '18 at 14:17
  • There is `class` the keyword and then there is a C++ class. There is also `struct` the keyword, but you don't have struct types in C++ like you do class types. Instead, both the `class` and `struct` keywords declare the same thing: a class. – hegel5000 May 08 '18 at 14:21
  • 3
    @DeiDei And that compatibility would be served fine by keeping C++ structs as C structs. Your backwards compatibility point is totally flawed. – juanchopanza May 08 '18 at 14:23
  • @juanchopanza How is it flawed? The fact that the default access for a C++ struct is `public` just like in C is most probably with backwards compatibility in mind. Not removing the keyword entirely in favor of `class` also has backwards compatibility all over it. The fact that a C++ struct is extended to have the capabilities of a class doesn't change the reason for keeping the keyword in the language. Isn't that what we are debating? Or are we mistaking backwards and forwards compatibility... – DeiDei May 08 '18 at 14:27
  • There are many reasons for why you can't compile (all of) C with a C++ compiler any more, but the `struct` is not one of them. And it's very common to define simple PODs in C++. – molbdnilo May 08 '18 at 14:28
  • 2
    @DeiDei Oh OK, you didn't read the question. – juanchopanza May 08 '18 at 14:28
  • @juanchopanza I've read it multiple times, I'm still not exactly sure what's being asked. I think they are asking why C++ doesn't have a tag namespace and prohibit inheritance off of `struct`s. To which my answer would be that the language is complicated enough without having to add special rules for that. – Mgetz May 08 '18 at 14:33
  • Probably answered in Bjarne Stroustrup's [The Design and Evolution of C++](https://www.amazon.com/dp/0201543303). Unfortunately, I don't have access to it at the moment. – Eljay May 08 '18 at 14:35
  • 3
    @Eljay: Confirmed via direct quote on the dupe. – Lightness Races in Orbit May 08 '18 at 15:00

2 Answers2

2

I cannot see the reason for making a struct behave (almost) like a class in C++.

One of the reason - provide ability to inherit from a struct adding constructor/destructor etc and still it can be passed to C functions as C structure.

Slava
  • 43,454
  • 1
  • 47
  • 90
  • As long as standard layout is preserved (no `virtual`) this is legal and even a nice way to handle it – Mgetz May 08 '18 at 14:21
  • 1
    I don't think separating the definition of a C struct from classes would have prevented this. C++ could have simply been defined such that a base of a class can be either a class, or a C struct. – eerorika May 08 '18 at 14:45
  • @user2079303 if you think about when the language was first specified I think it would have been non-starter to do that. As it would have effectively forced C++ compilers to be massively over-complicated (at the time) and would have already hard the perception of the language. – Mgetz May 08 '18 at 14:49
  • @Mgetz I don't think it would have had any noticeable effect on complexity of the compilers (although, I might be wrong). Rather, I would have assume it had complicated the wording of the standard. – eerorika May 08 '18 at 14:54
1

and thus be able to a) compile legacy C code

But the way classes are specified, you can compile legacy C struct definitions in C++ (except for potential backward incompatibilities that aren't related to structs, such as the newly added keywords).

b) define simple PODs.

The ability to define simple PODs wasn't prevented by the OOP features that C++ classes introduced. You can define POD classes in C++.

Both a) and b) were achieved without separating the meaning of struct and class, so you have not demonstrated an argument for doing so.

The concept of object oriented class is an extension of the concept of composite datatype, which is what C structs are. There was no necessity to complicate the language by adding a separate definition for C structs since C++ classes are sufficient for that purpose. Simplicity is a great trait to strive for in a language - especially when the language is so very lacking in that regard.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    I think the point is that C++ makes `struct` basically the same as `class`, so a) it doesn't prevent you from defining C-incompatible "structs" and b) it serves little purpose except to create confusion. – juanchopanza May 08 '18 at 14:48
  • @juanchopanza I think that assumes the premise that C++ is just a superset of C, this is already not the case explicitly in the standard. – Mgetz May 08 '18 at 14:50
  • 1
    "The ability to define simple PODs wasn't prevented by the OOP features that C++ classes introduced" -> that wasn't my claim. I know that I can define PODs with classes or structs in C++ as it is. And introducing a keyword from an ancestor language (`struct`) and at the same time augmenting its syntax so that it behaves (almost) like another keyword (`class`) isn't exactly adding to simplicity. – Angle.Bracket May 08 '18 at 15:03
  • 1
    @Angle.Bracket having two keywords with nearly same meaning is very, very much simpler than having two separate concepts for structs and classes where classes are everything that structs are, but more. High level abstract concepts are complex to specify for a language. Keywords are simple. More sensible reaction - in my opinion - to your argument would be: *"Why was the keyword `class` introduced at all instead of simply using `struct` for all classes (which I guess would then have been called structs)?"*. – eerorika May 08 '18 at 15:11
  • @user2079303: I can see your point. I am now aware that all of this has already been discussed in the question to which this one is a duplicate. Worth to have a look at also for the Stroustrup quote. – Angle.Bracket May 08 '18 at 15:16
  • @Mgetz I make no such assumptions. I'm not sure where that came from. – juanchopanza May 08 '18 at 17:36