Tell me more ×
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 we all know the class. Both are capable of holding functions and variables. some differences are
1. Class is given memory in heap and struct is given memory in stack
2. in class variable are private by default and in struct thy are public

My question is that struct was somehow abandoned for Class. Why? other that abstraction, a struct can do all the same stuff a class does. Then why abandon it?

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

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

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
5  
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. – Osama Rao Jul 30 '11 at 14:28
1  
@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
@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
@quant: Objects with data but no behaviour are an OO antipattern. In C it wouldn't really matter, but in C++ (or worse, C#) it raises red flags about the program design. – Aaronaught Jul 31 '11 at 16:35
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

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.