Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #11099
| Newsgroups | comp.programming |
|---|---|
| Date | 2020-07-25 15:31 -0700 |
| References | <f53949d7-80a0-44ec-a157-ff7a82f31bb8o@googlegroups.com> <rffmnv$djq$1@dont-email.me> <c093df03-f588-40a7-845e-4e8846a1eba5o@googlegroups.com> |
| Message-ID | <736aa191-181e-41cb-bcdf-b3a0d6c075e8o@googlegroups.com> (permalink) |
| Subject | Re: when to use a struct(ure) or a class? |
| From | Öö Tiib <ootiib@hot.ee> |
On Saturday, 25 July 2020 01:29:33 UTC+3, G G wrote: > On Friday, July 24, 2020 at 6:18:42 PM UTC-4, David Brown wrote: > > On 25/07/2020 00:06, G G wrote: > > > when do you choose a structure over a class when the > > > language supports functions inside of the struct(ure) > > > definitions? > > > > > > > Are you thinking specifically of C++, which has both "struct" and > > "class" ? (Maybe you have other languages in mind, but that's the only > > one I know that fits your question.) C++ as a language doesn't care > > about "class" vs. "struct" - the only difference is the default > > visibility of the members (private vs. public). So it is a matter of > > programmer style. But a very common distinction is that types where the > > data members have an invariant - some relationship between them that > > should be kept - should be a "class" with private data members accessed > > via methods (or sometimes friend functions). A type where the data > > members are basically independent and can reasonably be accessed > > directly, would be a "struct" even if there are also member functions. > > If there are virtual functions, or inheritance involved, you almost > > certainly would call it a "class". > > > > So a simple x, y point structure would be a "struct", while something > > holding an array of data along with a current length would be a "class". > > the language i'm thinking about as swift. Swift allocates "struct" in stack and passes it by copying its value; "class" it allocates dynamically and passes by (ref-counted) reference. So you choose "struct" or "class" depending on what way you want the language to manage it.
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
when to use a struct(ure) or a class? G G <gdotone@gmail.com> - 2020-07-24 15:06 -0700
Re: when to use a struct(ure) or a class? David Brown <david.brown@hesbynett.no> - 2020-07-25 00:18 +0200
Re: when to use a struct(ure) or a class? G G <gdotone@gmail.com> - 2020-07-24 15:29 -0700
Re: when to use a struct(ure) or a class? David Brown <david.brown@hesbynett.no> - 2020-07-25 00:52 +0200
Re: when to use a struct(ure) or a class? Öö Tiib <ootiib@hot.ee> - 2020-07-25 15:31 -0700
Re: when to use a struct(ure) or a class? Julio Di Egidio <julio@diegidio.name> - 2020-07-26 02:11 -0700
Re: when to use a struct(ure) or a class? Öö Tiib <ootiib@hot.ee> - 2020-07-27 08:37 -0700
Re: when to use a struct(ure) or a class? Julio Di Egidio <julio@diegidio.name> - 2020-07-25 00:02 -0700
Re: when to use a struct(ure) or a class? "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2020-07-25 09:16 +0200
Re: when to use a struct(ure) or a class? Julio Di Egidio <julio@diegidio.name> - 2020-07-25 00:30 -0700
csiph-web