In C++ and other influenced languages there is a construct called Structure (struct), and another called the class. Both are capable of holding functions and variables. Some differences are:

  1. Class is given memory in the heap and struct is given memory in the stack (remark: this is wrong for C++, but maybe correct in what the OP called "influenced languages")
  2. Class variable are private by default and in struct they are public

My question is: was the struct somehow abandoned for Class? If so, why? Other than the differences above, a struct can do all the same things that a class does. So why abandon it?

  • by abandon I meant why one is used over the other. – prometheuspk Jul 30 '11 at 13:14
  • your question answers this itself I think. – Umair A. Jul 30 '11 at 14:26
  • 4
    The difference between classes and structs is language-dependent. Some lessons from C++ do not really apply to C#. – Job Jul 30 '11 at 15:56
  • 7
    Uhhhh. In C++ you can allocate objects on the stack or heap. Wherever you want. – jojo Jul 31 '11 at 12:25
  • 8
    Wrong, wrong, wrong. Stack vs. heap has nothing to do with the difference. – Aaronaught Jul 31 '11 at 13:28

It is not abandoned at all. In fact, even modern languages like C# which make heavy use of class still offer you struct. As for when it's useful to choose one over the other, I refer you to this article:

Choosing Between Classes and Structures

Quoted from the MSDN article:

Consider defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

Do not define a structure unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.
  • 4
    This answer is really misguided as .Net/C# struct/class meaning is different than C++ one: they use the same name but have different semantic depending on the language!!! In C++ there is almost no difference between struct and class and the question is totally wrong on the first point, which is right in c# and D but not in c++. So the link to this article, which is about .Net, is really really wrong. It's not C++! – Klaim May 28 '13 at 8:31
  • @Klaim technically its not right for C# either (structs can be on the heap) The important distinction in C# is between value semantics and reference semantics, a distinction that C++ doesn't need as it can have values or references for anything – jk. Oct 29 '14 at 12:30

You are mistaken about C++: the only significant difference between class and struct is the default access specifier difference. Struct and class are for all intents and purposes synonyms, I believe struct is kept around for backwards compatibility to C.

  • 7
    Not only. Struct is a good choice when you just want to dump a bunch of data in one object and don't add any behaviour to it. – quant_dev Jul 30 '11 at 11:52
  • 3
    @quant: That's a terrible reason to use a struct. I really hope that comment was tongue-in-cheek. – Aaronaught Jul 31 '11 at 13:29
  • 1
    @Aaronaught Why? My view on the usage of struct is fairly standard, see this answer for example: stackoverflow.com/questions/54585/… – quant_dev Jul 31 '11 at 16:13
  • 1
    @Aaronaught How do you return multiple values then? Say, a procing model returns the clean price, dirty price, and sum of accrued payments. – quant_dev Jul 31 '11 at 17:45
  • 1
    @quant_dev - you can decide to attach some meta-meaning to a struct for your purposes (and for former C programmers one that actually does make a lot of sense), but that does not change the fact that the only difference between the struct and class keywords in C++ is the default access. – Joris Timmermans Aug 3 '11 at 9:33

the language D has created a greater distinction between class and struct

a struct in d is nothing but a stack allocated data record with some functions you can call on it (there is no option for inheritance unless you use a enum + union setup i.e. implement the polymorfism yourself) that is passed by value

a class is like we are used to: virtual functions, heap allocation, (single) inheritance, passed by ref

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.