12

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?

8
  • by abandon I meant why one is used over the other. Jul 30, 2011 at 13:14
  • your question answers this itself I think.
    – Umair A.
    Jul 30, 2011 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, 2011 at 15:56
  • 7
    Uhhhh. In C++ you can allocate objects on the stack or heap. Wherever you want.
    – jojo
    Jul 31, 2011 at 12:25
  • 8
    Wrong, wrong, wrong. Stack vs. heap has nothing to do with the difference.
    – Aaronaught
    Jul 31, 2011 at 13:28

3 Answers 3

11

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.
2
  • 5
    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, 2013 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, 2014 at 12:30
19

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.

10
  • 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, 2011 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, 2011 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, 2011 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, 2011 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. Aug 3, 2011 at 9:33
2

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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