Structs vs Classes - What's the purpose?

If structs and classes are effectively identical, except for the default public/private declarations, what's the purpose of having both? Why muddy the water?

Seems to me the only thing you need to do is add "public:" to the top of a class to make it a struct.
I think structs are relic from C and there for backward compatibility
there are rumors that a few early c++ compilers translated to C first and compiled that, and that in those days a struct was a C struct only. I don't recall this, or know if it is accurate (?).
Its been virtually identical to class for a long, long time.
I have always hated the word class, for that matter. A class is a group of kids with spitballs...
there are rumors that a few early c++ compilers translated to C first and compiled that, and that in those days a struct was a C struct only.

Not a rumor. It's quite true. Early C++ compilers were in fact a preprocessor that produced C code, then compiled the C code. Classes did indeed become structs. This is why C++ and C syntax are so closely linked, although that is starting to diverge.

That is also why we have the this pointer. It provided default addressability to the struct. The this pointer was always the first argument to any called member function. Today, we don't see the this pointer as an explicit argument, but it's still there. It's implicitly pushed on the stack.
Last edited on
Bjarne Stroustrup wrote:
The first C++ compiler (Cfront) was written in C++. To build that, I first used C to write a ``C with Classes''-to-C preprocessor. ``C with Classes'' was a C dialect that became the immediate ancestor to C++. That preprocessor translated "C with Classes" constructs (such as classes and constructors) into C. It was a traditional preprocessor that didn't understand all of the language, left most of the type checking for the C compiler to do, and translated individual constructs without complete knowledge. I then wrote the first version of Cfront in "C with Classes".

Cfront was a traditional compiler that did complete syntax and semantic checking of the C++ source. For that, it had a complete parser, built symbol tables, and built a complete internal tree representation of each class, function, etc. It also did some source level optimization on its internal tree representation of C++ constructs before outputting C. The version that generated C, did not rely on C for any type checking. It simply used C as an assembler. The resulting code was uncompromisingly fast. For more information, see D&E.

https://stroustrup.com/bs_faq.html#bootstrapping

Last edited on
Structs vs Classes - What's the purpose?

The history of how the two languages -- C & C++ -- evolved, one from the other shows "the purpose."

C is a general purpose, imperative procedural programming language. Program data is manipulated by lots and lots of functions that exist separate from the data. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support. Despite the low-level capabilities C was designed to encourage cross-platform programming. Same code, different hardware and operating systems.

C++ was created to be a programming ecosystem that encouraged using object-oriented design. An organic way of organizing programs. Data and procedures (functions) could be combined into a single object for storage and manipulation.

Another early design idea was "type-safety". Having the compiler flag as errors trying to assign one type (a string, for instance) to a variable of another, differing type. Say, an int.

C++ was originally tagged as "C with classes." Using C's struct as the basis for for what became classes in C++.

there are rumors that a few early c++ compilers translated to C first and compiled that

Yup, "Cfront." Saint Bjarne's original C++ compiler that converted C++ code into C and then compiled that to machine code.

More than a few people complain/wonder about why this feature is in the language, etc. C++ they say is like a Frankenstein's Monster, cobbled and stitched together from bits and pieces swiped from elsewhere.

At first C++ was the creation of a single person, Bjarne Stroustrup. Now it is result of a committee. A giraffe is a horse designed by a committee.

C++ is revised and updated with a view for solving real world problems. Does it do it as well as it could. Debatable.

Long story short, in C++ structs and classes are red-headed step children because of the history of how C++ evolved and mutated because and from C.
CFront used C as a "portable assembler", that's why it compiled to C; for portability.
So the short is that structs are a holdover from C. And maybe if I want to use C, I might need to use structs rather than classes. And if I'm using C++, I can just use classes.

The long is that it's a complicated history of evolution and backward compatibility, which I completely understand. Once you make functionality available and it gets used, you can't just remove it.
I use structs in C++ code when I want to combine disparate type variables into a common container and don't need any functions to manipulate the data.

You can't use classes in C code, you have to use structs. In C++ use either. In C++ a struct can contain functions.

Why? Because "history."

Once you make functionality available and it gets used, you can't just remove it.

Not exactly true. C++ has deprecated "stuff" (deprecated suggests "don't use"). and in later standards actually removed some of it.

std::random_shuffle for example. Deprecated in C++14, removed from C++17.

https://en.cppreference.com/w/cpp/algorithm/random_shuffle

std::shuffle is the replacement algorithm, requires a C++ random engine instance to work. std::random_shuffle used the C library random generator.

Why the change? Discussing THAT could start a Programming Holy War!
struct came from C - the overriding design goal being "as close to C as possible, but no closer".
class came from Simula 67 - the first programming language to support the object-oriented programming paradigm.
@SlowCoder, if you really want to get confused go look at WinAPI Desktop code, what used to be known as Win32. There's a key component of creating Windows GUI apps known as the "Window Class."

It's just a pre-defined struct as part of the WinAPI Frankenstein Monster, 'cuz the API is C-based.
Both classes and structs are similar in C++, however, classes offer more flexibility in terms of security.
No. The difference as per previous posts is the default public/private.
Note that (rightly or wrongly) it is a common convention amongst C++ programmers to use struct for bundles of dumb data (i.e. just some member variables) and class for more elaborate objects that do more than just hold some data.

This is a convention and nobody has to do this, but code is written to communicate intention and this is a common communication used by C++ programmers. You don't have to do it, but you will see it used.
Yes - when there are no private members I tend to use struct even if there are constructor(s) (eg when used with .emplace() etc)and class when there are private members.
I linked the core guidelines, for such things as invariants.

here is the link again:

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-class
Last edited on
Topic archived. No new replies allowed.