Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
  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?

share|improve this question
    
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. –  Neutralizer Jul 30 '11 at 14:26
3  
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
6  
Uhhhh. In C++ you can allocate objects on the stack or heap. Wherever you want. –  jojo Jul 31 '11 at 12:25
7  
Wrong, wrong, wrong. Stack vs. heap has nothing to do with the difference. –  Aaronaught Jul 31 '11 at 13:28
add comment

3 Answers

up vote 10 down vote accepted

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.
share|improve this answer
    
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
add comment

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.

share|improve this answer
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
    
+1 @quant_dev I like your way of thinking. –  prometheuspk Jul 30 '11 at 14:28
2  
@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
show 6 more comments

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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