8

Possible Duplicate:
C/C++ Struct vs Class

I know the technical differences between a struct and a class; and of course that question has been asked before.

Object-oriented programming relates objects and classes. In C++ taxonomy, is a struct also a class?

Community
  • 1
  • 1
user2023370
  • 10,488
  • 6
  • 50
  • 83
  • I want to mark this duplicate of http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c, but actually that question and its answers don't come right out and say that a class defined with `struct` is just as much a class as a class defined with `class` is. In that sense, the question "what are the differences between a struct and a class" is already wrong-headed, it implies there are two kinds of types. It should be "what are the differences between keyword struct and keyword class", since in C++ there isn't really any such thing as "a struct". – Steve Jessop Apr 11 '11 at 10:30
  • 1
    I read both questions prior to asking, and believe it is not a duplicate; hopefully a simple, *specific*, and useful question; for me at least. This is a question of *taxonomy*. *Is* a C++ "struct", also a "class"? Thanks. – user2023370 Apr 11 '11 at 10:45
  • This is not an *exact* duplicate of either [C/C++ Struct vs Class](http://stackoverflow.com/questions/2750270/c-c-struct-vs-class) or [What are the differences between struct and class in C++](http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c). – johnsyweb Apr 11 '11 at 12:04
  • @user643722: in that case the answer you want is 9/4: "A *structure* is a class...". Any further information about the differences is waffle from your POV. Beware using this definition strictly, though. I think the word "structure" is used inadvisedly in (for example) 17.1.8, I'm not sure I believe the standard really intends to say that it's undefined behavior for the `traits` template argument to be defined with `class`. Also when the standard mentions "data structures" it usually means classes, not structures in this sense. – Steve Jessop Apr 11 '11 at 21:35
  • 1
    There is also a difference when predeclaring the class/struct: If a struct is defined as `struct A { ... };`, then when it is predeclared, it needs to be predeclared as `struct A;`, also not `class A;`. Some compilers (e.g. MSVC) distinguish use different name mangling for `class` or `struct` members. – tmlen May 22 '16 at 23:02

4 Answers4

8

Yes, it's a full-blown class - struct keyword is a syntactic sugar that makes all members publicly accessible by default, while they are private by default in a class.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 1
    +1. The syntax is still there, mainly, to allow full compatibility with plain C. – salezica Apr 11 '11 at 10:14
  • Thanks sharptooth. I know these differences. I guess you're instinct is that through being so similar, a struct is also a class. Is that also the position taken in the C++ standard? One reason I ask is that I've seen comments correcting an author's reference to a struct as a class. – user2023370 Apr 11 '11 at 10:49
  • Is there a difference regarding the way both occupy memory? – Salman A Apr 11 '11 at 10:58
  • 1
    @Salaman: No. They are the same. This is not C#. – johnsyweb Apr 11 '11 at 11:03
  • 1
    @user643722 - This is mostly a historical accident. Once upon a time, classes were added to C, creating the language "C with classes". This language eventually evolved into what we now know as C++. Somewhere along the way, someone one day asked "Why can't we have member functions in a struct?". And the reply was "No reason." So, there we are. – Bo Persson Apr 11 '11 at 12:15
  • @Bo: Yes, I have heard that old saw many times. I am curious as to whether, *now*, regarding C++, a struct *is* (also) a class. – user2023370 Apr 11 '11 at 15:10
  • @user643722 - That was for the history lesson, why we have both class and struct. Right now a struct is like a class, only with public access and public inheritance as defaults. – Bo Persson Apr 11 '11 at 15:14
  • @Bo: Thanks. So you would answer, no? – user2023370 Apr 11 '11 at 17:46
4

Yes. The ONLY difference is that by default in a class everything is private, and in a struct, by default everything is public. The difference, in that sense is purely syntactical.

satnhak
  • 9,407
  • 5
  • 63
  • 81
3

Taxonomically, yes. Other than their different default access specifiers, they are exactly the same in C++.

  • All members and attributes of a struct are public by default.

  • All members and attributes of a class are private by default.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
3
  1. The member's of a struct are public by default
  2. The default inheritance for a class is private while for a struct it is public
Sriram Subramanian
  • 2,623
  • 5
  • 32
  • 35