471,627 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

Structures v/s Classes

Hi,

can anyone give specific examples where structs are preferred to classes?
Feb 3 '06 #1
2 1288
Kiran A K a écrit :
Hi,

can anyone give specific examples where structs are preferred to classes?


I'm not a master of code, but I use struct for grouping variables withoutneed
of methods.

Like Event data and more generally composite parameters.

cyrille

Feb 3 '06 #2
# Cyrille37 # wrote:
can anyone give specific examples where structs are preferred to classes?


I'm not a master of code, but I use struct for grouping variables without need
of methods.

Like Event data and more generally composite parameters.


IMO, that's not a good way of deciding whether to use a struct or a
class. What you should be looking at is the semantics of structs and
classes, and that doesn't affect whether or not you have methods.

For me, the questions are (at least):

1) Is this a sort of low level "atomic" type? That would *suggest* a
value type (struct)
2) Is the data potentially large? (Direct data only) That would
*suggeste* a reference type (class)
3) Do you need the type to be mutable? That would almost always suggest
a reference type.

It's more than that, and you tend to get a feel for it, but those are
good starting questions IMO. The "default" position should almost
always be to use a class. I very rarely introduce my own structs.

Jon

Feb 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by OKB (not okblacke) | last post: by
11 posts views Thread by Fred Bennett | last post: by
1 post views Thread by kazack | last post: by
6 posts views Thread by nick | last post: by
6 posts views Thread by Ken Allen | last post: by
7 posts views Thread by OpticTygre | last post: by
14 posts views Thread by pmclinn | last post: by
2 posts views Thread by thomasfarrow | last post: by
1 post views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.