Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a struct doesn't belong in an object oriented program

Tags:

c++

oop

Or does it?

Should an object-oriented design use a language construct that exposes member data by default, if there is an equally useful construct that properly hides data members?

EDIT: One of the responders mentioned that if there's no invariant one can use a struct. That's an interesting observation: a struct is a data structure, i.e. it contains related data. If the data members in a struct are related isn't there's always an invariant?

like image 516
andreas buykx Avatar asked Oct 30 '08 21:10

andreas buykx


1 Answers

In C++, structs and classes are identical except for the default public/privateness of their members. (This default is easily, and usually, overridden.)

However, most programmers think of a struct as a "data object" and a class as an "interactive object". That's not a bad thing; and in fact should be taken advantage of. If something is just an inanimate lump of data (even maybe if it has a couple of inspector methods), use a struct for it; it'll save a bit of effort when a programmer is trying to see what it's for.

like image 110
Artelius Avatar answered Sep 28 '22 07:09

Artelius