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.

C and most likely many other languages provide a struct keyword for creating structures (or something in a similar fashion). These are (at least in C), from a simplified point of view like classes, but without polymorphism, inheritance, methods, and so on.

Think of an object-oriented (or multi paradigm) language with C-style structs. Where would you choose them over classes? Now, I don't believe they are to be used with OOP as classes seem to replace their purposes, but I wonder if there are situations where they could be preferred over classes in otherwise object-oriented programs and in what kind of situations. Are there such situations?

share|improve this question
    
Which language are you using? What are you publishing? Most modern OO languages, except Java, support properties in one way or another. –  kevin cline Feb 28 '11 at 20:16

6 Answers 6

up vote 15 down vote accepted

There's a distinction I've seen in some books between state-carrying objects and functionality-providing objects. The exact terms differ from source to source. But generally speaking, the former are distinguished by having a bunch of fields and a whole bunch of simple getters-setters pairs (and maybe a constructor).

These are particularly common in Java, for classes that are explicitly marked as "serializable".

I would argue that this sort of classes make the most sense as structs, if you feel that there is no need to directly hide the members (e.g., in the case of nested types).

share|improve this answer
    
Robert C Martin calls them "data transfer objects" in Clean Code. –  user16764 Mar 22 '12 at 16:40

In C, there are no classes, so struct is the way to bundle data. In C++, struct is the same as class except that inheritance and member access is public by default rather than private. C# also has a sort of struct, but I don't know enough C# to comment on it. Common Lisp has the defstruct form, which works as much like C structs as it can, considering the language difference, and it is different from a Common Lisp Object System class.

There is, however, a conceptual difference between a related bundle of data and a real class, and in C++ struct is often used for a bundle of data, while class is used for objects that are supposed to have their own behavior. As a guideline, I'd say that a class represents something you can say something useful about, such as a class invariant, while a struct would be more loosely coupled with data values not really depending much on other ones.

share|improve this answer
5  
The difference between a struct and a class is more significant in C# - the big thing being that a struct is a value type and a class is a reference type. Microsoft recommends not using a struct if the contents will be bigger than 16 bytes or so. –  Carson63000 Mar 1 '11 at 0:00

On a very basic level what's the difference between a struct and a pure data class (one that has no functionality apart from property accessors)?

Nothing but the memory footprint really.

share|improve this answer

The accepted answer is good. I'd just like to add....

Sometimes it's better to put a method inside a class. Other times it's better to have an external function where the struct/class is passed in as a parameter (even if you are doing OOP style).

example of when to put a method inside the class: Rectangle.CaclulateArea()

example of when to put a function outside the class: Canvas.Draw(Rectangle rect)

you could put Draw() inside the Rectangle and it would work. But drawing is a tier dependent operation. You wouldn't draw on the server side, but on the client side. Draw() would not make sense to even exist on the server. However "CalculateArea()" is independent of tier and is a true attribute of the rectangle, so it's better to include it as a member method.

share|improve this answer

For me, the big difference is whether there is a class-invariant or not.

Or in other words: am I free to change any field to any value at any point in time? If so, it's a struct; If not, it's a class.

The usual example of a struct is a 2D point, with just an X and Y member, that can have any value and are totally independent of each other. Public access is acceptable.

An example of a class is the combination of a pointer to a character array and a length field: the length depends on the value of the pointer, and thus it should not be a struct. The fields should be private, and access should go through setters and getters to ensure the class invariant.

share|improve this answer

1) Network structs: structures that are transmitted over the wire.

More generally and formally, any class that you require to be POD, trivial, standard-layout, or a related concept. For an excellent discussion of these concepts, with examples, see this answer (it is for C++11).

2) Functors (predicates in particular), that you would pass over to functions like std::remove_if. Otherwise put, classes similiar to / inherited from std::binary_function or the like.

3) Meta-programming concepts, which may contain only public constexprs, and/or typedefs.

Edit: Note: This answer applies to C++ classes vs structs.

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.