7

Possible Duplicate:
What are the differences between struct and class in C++

If the only difference between structs and classes is the default access specifier(in C++), then why does C++ also have classes?

Community
  • 1
  • 1
Lockhead
  • 2,423
  • 7
  • 35
  • 48
  • see [struct vs. class](http://stackoverflow.com/questions/2750270/c-c-struct-vs-class) – mbx Apr 20 '11 at 20:47
  • http://stackoverflow.com/questions/543515/structs-vs-classes-in-c seems like it might be relevant, among others. – Jimmy Sawczuk Apr 20 '11 at 20:48
  • @greg I'm not asking what the differences are... – Lockhead Apr 20 '11 at 20:51
  • Without any backing facts and just by extrapolating, it probably was because initially, `class` and `struct` were distinct (probably that classes only had methods). If it's the case, then when they added methods to `struct`s, they would have had to support both classes and struct to remain backwards-compatible with both C and older C++. – zneak Apr 20 '11 at 20:53
  • See also [In C++, why struct is in fact class?](http://stackoverflow.com/questions/4427759/in-c-why-struct-is-in-fact-class) or [C/C++ Struct vs Class](http://stackoverflow.com/questions/2750270/c-c-struct-vs-class). – Greg Hewgill Apr 20 '11 at 20:56
  • 1
    @JohnMcG someone is obviously mad. – Lockhead Apr 20 '11 at 21:31

4 Answers4

17

First, it's backward compatibility with C. Second (and more importantly), it helps describe your intent. The standard convention is that PODs should be denoted with struct and collections of data and behavior should be denoted with class.

Bill
  • 14,257
  • 4
  • 43
  • 55
  • Well then why are class-style actions allowed in structs, like operator overloading? Doesn't that break compatibility? – Lockhead Apr 20 '11 at 20:53
  • 1
    In this case, it's about *convention*. C++, in this and many other ways, lets you do lots of things, whether they are good ideas or not. – Joe Apr 20 '11 at 21:04
  • 2
    @MisterSir - no, **backwards** compatibility means that C code using structs would compile and work as expected under C++. Your example (struct with behavior) is a C++ code, and naturally it would not work under C as C++ does not guarantee **forward** compatibility – davka Apr 20 '11 at 21:06
  • Yes. It means that the _compiler_ is backward compatible in that it can (to some degree) compile old C code. It does not mean that your shiny new C++ code will work in a C compiler. – Lightness Races in Orbit Apr 24 '11 at 16:38
  • @davka: Strictly speaking, the reason it does not work under C is that C does not guarantee forward compatibility ;) – Lightness Races in Orbit Apr 24 '11 at 16:38
  • 3
    @Tomalak: I think one of C's biggest failings is that it wasn't created forward-compatible with C++0x. :-) – Bill Apr 24 '11 at 18:56
0

You want classes to have the default access specifier to be private when designing object-oriented hierarchies, but structs need to have the default access specifier to be public to preserve compatiblity with C.

Don Reba
  • 13,814
  • 3
  • 48
  • 61
0

Backwards compatibility with C. C didn't have classes, but it had structs.

Note that this is "backwards compatibility" in the text of the .c file only. In C++ structs are really classes, with a default exposure of public instead of a default exposure of private.

class Bob {
   // I haven't overridden the exposure, so it's private
   int age;
   int height;

   public:
     Bob();

};


struct Bob {
   // I haven't overridden the exposure, so it's public
   int age;
   int height;

   // note the constructor defaults to the "default" constructor.
};

A true C style struct is defined as so

extern "C" {
  struct Bob {
     int age;
     int height;
  };
}

In which case age becomes more of an alias of "offset +0" and height becomes more of an alias of "offset +(sizeof(int) + alignment to memory manager boundary) instead of being conceptual fields which can be implemented in any desired manner.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • -1 Nonsense; `extern "C"` is used to specify linkage. It is *not* a "drop into the C language" switch. e.g. `extern "C" { struct T { T() {} }; }` is perfectly valid. (The rest of your answer is correct.) – Lightness Races in Orbit Apr 24 '11 at 16:36
  • Where's the part in my post that declares "drop into the C language"? I merely stated that a "true C style struct is defined this way..." – Edwin Buck Apr 24 '11 at 16:39
  • Yes, which is not true. You're still declaring+defining a C++ struct. (Though I see now that perhaps you merely meant to talk about C-compatibility?) – Lightness Races in Orbit Apr 24 '11 at 16:43
0

As everyone else has posted, the main difference is default protection level (i.e. public or private). However, it's also a good indication on the actual use of the data structure. For example, sometimes you might want a light-weight structure, similar to a java bean. In that case you might just have:

struct point {
   float x, y;
};

It's clear that you're not using any C++ features here, versus:

class point {
public:
   float x, y;
};

It's less clear what your intention is. I'd likely note the second example as bad code because someone didn't provide getters and setters.

Abe Schneider
  • 977
  • 1
  • 11
  • 23