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:
- 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") - 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– prometheuspk2011年07月30日 13:14:40 +00:00Commented Jul 30, 2011 at 13:14
-
your question answers this itself I think.Umair A.– Umair A.2011年07月30日 14:26:11 +00:00Commented Jul 30, 2011 at 14:26
-
4The difference between classes and structs is language-dependent. Some lessons from C++ do not really apply to C#.Job– Job2011年07月30日 15:56:40 +00:00Commented Jul 30, 2011 at 15:56
-
7Uhhhh. In C++ you can allocate objects on the stack or heap. Wherever you want.jojo– jojo2011年07月31日 12:25:42 +00:00Commented Jul 31, 2011 at 12:25
-
8Wrong, wrong, wrong. Stack vs. heap has nothing to do with the difference.Aaronaught– Aaronaught2011年07月31日 13:28:18 +00:00Commented Jul 31, 2011 at 13:28
3 Answers 3
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.
-
5This 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– Klaim2013年05月28日 08:31:52 +00:00Commented 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 anythingjk.– jk.2014年10月29日 12:30:20 +00:00Commented Oct 29, 2014 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.
-
7Not 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– quant_dev2011年07月30日 11:52:46 +00:00Commented 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– Aaronaught2011年07月31日 13:29:55 +00:00Commented 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– quant_dev2011年07月31日 16:13:36 +00:00Commented 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– quant_dev2011年07月31日 17:45:52 +00:00Commented 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.Joris Timmermans– Joris Timmermans2011年08月03日 09:33:57 +00:00Commented Aug 3, 2011 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