Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #11088 > unrolled thread

when to use a struct(ure) or a class?

Started byG G <gdotone@gmail.com>
First post2020-07-24 15:06 -0700
Last post2020-07-25 00:30 -0700
Articles 10 — 5 participants

Back to article view | Back to comp.programming


Contents

  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

#11088 — when to use a struct(ure) or a class?

FromG G <gdotone@gmail.com>
Date2020-07-24 15:06 -0700
Subjectwhen to use a struct(ure) or a class?
Message-ID<f53949d7-80a0-44ec-a157-ff7a82f31bb8o@googlegroups.com>
when do you choose a structure over a class when the 
language supports functions inside of the struct(ure)
definitions?

[toc] | [next] | [standalone]


#11089

FromDavid Brown <david.brown@hesbynett.no>
Date2020-07-25 00:18 +0200
Message-ID<rffmnv$djq$1@dont-email.me>
In reply to#11088
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".

[toc] | [prev] | [next] | [standalone]


#11090

FromG G <gdotone@gmail.com>
Date2020-07-24 15:29 -0700
Message-ID<c093df03-f588-40a7-845e-4e8846a1eba5o@googlegroups.com>
In reply to#11089
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.

[toc] | [prev] | [next] | [standalone]


#11091

FromDavid Brown <david.brown@hesbynett.no>
Date2020-07-25 00:52 +0200
Message-ID<rffon0$mnt$1@dont-email.me>
In reply to#11090
On 25/07/2020 00:29, 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.
> 

OK.  I don't know Swift at all, so I can't say if people use a similar
convention there.

It's nice to see real posts and real questions in this newsgroup, but I
suspect that you'll get more concrete answers in a dedicated Swift
newsgroup or Apple programming group (I don't think anyone else uses
Swift, do they?).

[toc] | [prev] | [next] | [standalone]


#11099

FromÖö Tiib <ootiib@hot.ee>
Date2020-07-25 15:31 -0700
Message-ID<736aa191-181e-41cb-bcdf-b3a0d6c075e8o@googlegroups.com>
In reply to#11090
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.

[toc] | [prev] | [next] | [standalone]


#11100

FromJulio Di Egidio <julio@diegidio.name>
Date2020-07-26 02:11 -0700
Message-ID<4c83f961-14e3-4e86-b05d-9a0c50ce8b95o@googlegroups.com>
In reply to#11099
On Sunday, 26 July 2020 00:31:05 UTC+2, Öö Tiib  wrote:
> 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). [...]

As said, that is essentially bullshit even in C++.

> > > 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".

That would be an *instance* of a class, by the way.

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

It might be allocated on a magnetic tape for all that matters:
the primary difference is *semantical*, how *assignment* works
in particular: it is certainly not yet another implementation
detail, nor it is the passing of arguments by reference vs by
value, despite some analogy, as one can indeed pass a value
type by refence, or vice versa.  --  When Dunning-Kruger is
a compliment.

Anyone else wants a review?  You are welcome...

Julio

[toc] | [prev] | [next] | [standalone]


#11101

FromÖö Tiib <ootiib@hot.ee>
Date2020-07-27 08:37 -0700
Message-ID<e42796d8-0d79-4754-948c-0acba53fb4f2o@googlegroups.com>
In reply to#11100
On Sunday, 26 July 2020 12:11:18 UTC+3, Julio Di Egidio  wrote:
> On Sunday, 26 July 2020 00:31:05 UTC+2, Öö Tiib  wrote:
> > 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). [...]
> 
> As said, that is essentially bullshit even in C++.

What is bullshit? It was correct.
C++ does not make difference between class and struct about type
anywhere but in class definition and there it only alters what is
the implicit access level of base classes and members.
Demo: <http://coliru.stacked-crooked.com/a/075835e7af83e0f7>

> > > > 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".
> 
> That would be an *instance* of a class, by the way.
> 
> > > 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.
> 
> It might be allocated on a magnetic tape for all that matters:
> the primary difference is *semantical*, how *assignment* works
> in particular: it is certainly not yet another implementation
> detail, nor it is the passing of arguments by reference vs by
> value, despite some analogy, as one can indeed pass a value
> type by refence, or vice versa.

It is not about assignment. Assignment is just subset of *passing*.
And it is pointless to *pass* local objects in stack by reference
as those will be dangling references moments later. So Swift
simply does not let you to choose to do that error. Choice is
how you want it to handle the instances is made when you define
it as class" or "struct". 

> --  When Dunning-Kruger is a compliment.

Perhaps go back to your Javascript and stop posting nonsense about
compiled languages.
 
> 
> Anyone else wants a review?  You are welcome...
> 
> Julio

[toc] | [prev] | [next] | [standalone]


#11092

FromJulio Di Egidio <julio@diegidio.name>
Date2020-07-25 00:02 -0700
Message-ID<55be66ab-3b98-4137-9ae5-1b5ef7e3cb81o@googlegroups.com>
In reply to#11088
On Saturday, 25 July 2020 00:06:32 UTC+2, G G  wrote:

> when do you choose a structure over a class when the 
> language supports functions inside of the struct(ure)
> definitions?

<https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html>

To choose, mainly consider the reference type vs value type distinction...

Julio

[toc] | [prev] | [next] | [standalone]


#11093

From"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Date2020-07-25 09:16 +0200
Message-ID<rfgm8r$ude$1@gioia.aioe.org>
In reply to#11088
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?

It is difficult to answer without reference to a concrete language.

Structure is a construct to create a container type with certain 
properties, such as static named association of container elements, 
record aggregates etc.

Class is a set of types. Members of the class could be types created as 
structures.

Any type should have a class rooted in the type.

Functions do not belong inside anything but compilation modules. E.g. if 
a function operates on two different classes where would you put it?

So basically there should be nothing to choose from.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

[toc] | [prev] | [next] | [standalone]


#11094

FromJulio Di Egidio <julio@diegidio.name>
Date2020-07-25 00:30 -0700
Message-ID<7587b8c0-0af2-4726-aef2-01826720d636o@googlegroups.com>
In reply to#11093
On Saturday, 25 July 2020 09:16:47 UTC+2, Dmitry A. Kazakov  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?
> 
> It is difficult to answer without reference to a concrete language.

He has already clarified that it's Swift, and I had just now answered
the question: fucking moron...

> So basically there should be nothing to choose from.

Which remains wrong: there is almost nothing to choose *in C++ only* of
all the languages, and not even there usually, i.e. as long as one wants
the code to speak for itself.

Fucking incompetent spammers, trolls and liars all over the place...

*Plonk*

Julio

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming


csiph-web