Why Struct if we have classes

my question is why to use struct. if i have classes that i can
hold with them not only different data types but also functions and access specifiers
people tend to use them for small data structures with data in them and when no "behaviour" is associated with that data i.e. no methods.

also, stuff in structs is public by default.
Last edited on
my question is why to use struct. if i have classes that i can
hold with them not only different data types but also functions and access specifiers

You can do that with structs, too. Structs can have methods, and access specifiers. In fact, in C++, structs are identical to classes in every way, except that the default access specifer is public, not private.

Why are they in C++? Because they were in C, and C++ was designed to be as compatible with C as possible.

Why would you use them now, in C++? I suppose the main benefit of them is that, when you use a struct, it's a clear indication to other developers that the object is intended to be a collection of publically accessible data, rather than an entity with a well-defined interface that hides its implementation.
Last edited on
wow thanx for the explanation mikeyboy
You're welcome :)
closed account (Dy7SLyTq)
just one thing:
and C++ was designed to be as compatible with C as possible

thats not entirely true. if it were c++ would have weaker type checking (just one example). c++ is meant to be as compatible with c as makes sense, not as much as possible, because then it would retain to many (jimo) ugly features that were "fixed" in c++.
Topic archived. No new replies allowed.