2

In old day, in other words, when it comes to C age, struct doesn't have constructor, destructor, etc. However,nowadays(C++ age), struct has been expanded much more like class.

So, what's the purpose that C++ designer intend?

In detail, why add constructor, destructor, inheritance, member functions into struct when compared with c?

This is not a duplicate question to C/C++ Struct vs Class, because here the main point lie on the design purpose, not difference between struct and class.

It's also not duplicate to Why do both struct and class exist in C++? , I don't see the purpose of expanding struct under the question above.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
guo
  • 9,674
  • 9
  • 41
  • 79
  • 1
    There's no good reason to have both `struct` and `class`. Presumably `class` was added for OO coolness. – juanchopanza Jun 16 '16 at 09:28
  • 1
    This is a [FAQ question.](http://www.stroustrup.com/bs_faq.html#class) – uh oh somebody needs a pupper Jun 16 '16 at 09:31
  • @ecatmur I don't see any explanation in that question on why "class" exists alongside "struct". – uh oh somebody needs a pupper Jun 16 '16 at 09:32
  • 1
    There never was a time when class was something more than a struct in C++ (at least not in standard C++, I don't know much about pre-standard times, I admit). – eerorika Jun 16 '16 at 09:34
  • 3
    Related, maybe touches on the answer to this question: http://stackoverflow.com/questions/34906229/why-do-both-struct-and-class-exist-in-c/34907535#34907535 – eerorika Jun 16 '16 at 09:38
  • @user2079303 But one could imagine three different design scenarios: make `struct` mean the same as in C, remove `struct`, or don't introduce `class`. Instead we have basically two synonyms for a class. – juanchopanza Jun 16 '16 at 09:44
  • @juanchopanza I didn't mean to imply that the question is moot. The wording of the question could be interpreted to imply the opposite of my clarification, and my intention was simply to dispell such potential notion. – eerorika Jun 16 '16 at 09:50
  • Possible duplicate of http://stackoverflow.com/questions/1654444/why-is-there-a-class-keyword-in-c - @guo - do you see any significant difference in the questions? (wanting a different/better answer is not a valid reason not to close this Q - you can offer a reward on that answer instead, though might (?) need more rep first to do so) – Tony Delroy Jun 16 '16 at 10:05
  • @TonyD well, I dont think it's duplicate of your link, your link refers a question about "Why introduce class in c++", while mine is "Why expand the struct“. I dont think it's the same question. – guo Jun 16 '16 at 10:10
  • @guo - But the answers to the other question address your question as well. See http://stackoverflow.com/a/1654475/597607 – Bo Persson Jun 16 '16 at 10:20

1 Answers1

9

Let's explore the other possible design options, enumerated by juanchopanza in the comments.

remove struct

An advantage of this option is less confusion and non-existence of questions like this.

A drawback is the significant incompatibility with C, and I believe that compatibility with C was and still is one of the design goals of C++.

don't introduce class.

I consider this to refer to just the introduction of the new keyword. See the comments for another interpretation.

An advantage is also less confusion from arising from two names for the same thing.

A drawback is mismatch with common object oriented terminology.

make struct mean the same as in C

Stoustrup's quote from: http://www.drdobbs.com/c-theory-and-practice/184403384

Originally quoted from: Bjarne Stroustrup. The Design and Evolution of C++ (Addison-Wesley, 1994).

Maybe we could have lived with two sets of rules, but a single concept provides a smoother integration of features and simpler implementations. I was convinced that if struct came to mean "C and compatibility" to users and class to mean "C++ and advanced features," the community would fall into two distinct camps that would soon stop communicating... Only a single concept would support my ideas of a smooth and gradual transition from "traditional C-style programming," through data abstraction, to object-oriented programming. Only a single concept would support the notion of "you only pay for what you use" ideal.

He later added that:

I think the idea of keeping struct and class the same concept saved us from classes supporting an expensive, diverse, and rather different set of features that we have now. In other words, the "a struct is a class" notion is what has stopped C++ from drifting into becoming a much higher-level language with a disconnected low-level subset."

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • That quote is taken out of context. It comes from Chapter 3 page 76, right before that it says: "Then why did I not at this point choose to make structs and classes different notions? My intent was to have a single concept: a single set of layout rules, ... etc. [sic]" – uh oh somebody needs a pupper Jun 16 '16 at 10:23
  • 1
    @uhohsomebodyneedsapupper but does the original context differ from this (I don't have the book)? Isn't that exactly what we are speculating here? *What was the reason for design decision that structs and classes be the same, rather than let structs be C-structs, and classes be this new thing.* I think that the quote appears pretty self-sufficient, but if you think that it is worthwhile, then I would appreciate the addition of the preceding paragraph to the quote. – eerorika Jun 16 '16 at 10:35
  • I think your consideration of `don't introduce class` option leaves something to be desired. If you don't introduce classes at all i.e., no constructors and destructors, this means no RAII, and probably, no exceptions either as result -- C++ would be something completely different. Classes are an essential feature of C++ as we know it today, or even as we knew it in 1995. – Chris Beck Jun 16 '16 at 12:28
  • @ChrisBeck here, I considered "*don't introduce class*" to mean that structs would have been designed to be what they are now - with constructors and destructors - and simply that no new keyword would have been introduced and that the concept of a class would then have a different name - struct. It didn't occur to me to even consider a design that would have omitted essential OOP features exactly because that would have been so radically different from what C++ is. – eerorika Jun 16 '16 at 12:49
  • Ah I see. Makes more sense now. – Chris Beck Jun 16 '16 at 12:50