473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

conceptual difference between 'struct' and 'class' ???

Hi,

I don't get the difference between a struct and a class !

ok, I know that a struct is a value type, the other a reference type, I
understand the technical differences between both, but conceptually speaking
: when do I define something as 'struct' and when as 'class' ?

for example : if I want to represent a 'Time' thing, containing :
- data members : hours, mins, secs
- some constructors
- some operator overloading functions maybe

do it define it as a struct or as a class ? and why ?

many thanks
Chris
Nov 16 '05 #1
5 8711
Chris,

Personally, for me, I see structures as data storage units where there
is not a lot of business logic associated with them. Things like integers,
dates, etc, etc, all have very small rule sets, whereas classes have much
larger, more complex relations.

Also, it seems that you want something like the TimeSpan structure, as
you are storing hours, minutes, and seconds, which is pretty much what this
does (it stores the amount of time between two points in time).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris" <ch********@pandora.be> wrote in message
news:5v***********************@phobos.telenet-ops.be...
Hi,

I don't get the difference between a struct and a class !

ok, I know that a struct is a value type, the other a reference type, I
understand the technical differences between both, but conceptually
speaking
: when do I define something as 'struct' and when as 'class' ?

for example : if I want to represent a 'Time' thing, containing :
- data members : hours, mins, secs
- some constructors
- some operator overloading functions maybe

do it define it as a struct or as a class ? and why ?

many thanks
Chris

Nov 16 '05 #2
Use a class when object identity is more important than value.
Use a struct when the value contained by an instance is more important than
instance identity.

Structs are usually (not always) simpler types. Struct variables directly
contain their values, so when you pass a struct instance as a parameter, it
can be more expensive than passing an instance of a reference type, due to
the copying costs.

There are probably more, but these are the ones that come to mind.
--
Mickey Williams
Author, "Visual C# .NET Core Ref", MS Press
www.neudesic.com
www.servergeek.com

"Chris" <ch********@pandora.be> wrote in message
news:5v***********************@phobos.telenet-ops.be...
Hi,

I don't get the difference between a struct and a class !

ok, I know that a struct is a value type, the other a reference type, I
understand the technical differences between both, but conceptually speaking : when do I define something as 'struct' and when as 'class' ?

for example : if I want to represent a 'Time' thing, containing :
- data members : hours, mins, secs
- some constructors
- some operator overloading functions maybe

do it define it as a struct or as a class ? and why ?

many thanks
Chris

Nov 16 '05 #3
A struct type is a value type which means it cannot be null.
U¿ytkownik "Chris" <ch********@pandora.be> napisa³ w wiadomo¶ci
news:5v***********************@phobos.telenet-ops.be...
Hi,

I don't get the difference between a struct and a class !

ok, I know that a struct is a value type, the other a reference type, I
understand the technical differences between both, but conceptually speaking : when do I define something as 'struct' and when as 'class' ?

for example : if I want to represent a 'Time' thing, containing :
- data members : hours, mins, secs
- some constructors
- some operator overloading functions maybe

do it define it as a struct or as a class ? and why ?

many thanks
Chris

Nov 16 '05 #4
Some things about structs vs classes I haven't seen mentioned prior to me
"starting" this post:

Pulled from
"ms-help://MS.VSCC/MS.MSDNQTR.2003FEB.1033/vbcn7/html/vaconstructuresandclasses.h
tm"

Similarities
Structures and classes are similar in the following respects:

*Both are container types, meaning that they contain other types as members.
*Both have members, which can include constructors, methods, properties,
fields, constants, enumerations, events, and event handlers.
*Members of both can have individualized accessibilities. For example, one
member can be declared Public and another Private.
*Both can implement interfaces.
*Both can have shared constructors, with or without parameters.
*Both can expose a default property, provided that property takes at least
one argument.
*Both can declare and raise events, and both can declare delegates.
Differences
Structures and classes differ in the following particulars:

*Structures are value types; classes are reference types.
*Structures use stack allocation; classes use heap allocation.
*All structure members are Public by default; class variables and constants
are Private by default, while other class members are Public by default. This
behavior for class members provides compatibility with the Visual Basic 6.0
system of defaults.
*A structure must have at least one nonshared variable or event member; a
class can be completely empty.
*Structure members cannot be declared as Protected; class members can.
*A structure procedure can handle events only if it is a Shared Sub
procedure, and only by means of the AddHandler statement; any class procedure can
handle events, using either the Handles keyword or the AddHandler statement.
*Structure variable declarations cannot specify initializers, the New
keyword, or initial sizes for arrays; class variable declarations can.
*Structures implicitly inherit from the ValueType class and cannot inherit
from any other type; classes can inherit from any class or classes other than
ValueType.
*Structures are not inheritable; classes are.
*Structures are never terminated, so the common language runtime (CLR) never
calls the Finalize method on any structure; classes are terminated by the garbage
collector, which calls Finalize on a class when it detects there are no active
references remaining.
*A structure does not require a constructor; a class does.
*Structures can have nonshared constructors only if they take parameters;
classes can have them with or without parameters.
*Every structure has an implicit public constructor without parameters. This
constructor initializes all the structure's data members to their default values.
You cannot redefine this behavior.

Instances and Variables
Because structures are value types, each structure variable is permanently bound
to an individual structure instance. But classes are reference types, and an
object variable can refer to various class instances. This distinction affects
your usage of structures and classes in the following ways:

A structure variable implicitly includes an initialization of the members using
the structure's parameterless constructor. Therefore, Dim S As Struct1 is
equivalent to Dim S As Struct1 = New Struct1().
When you assign one structure variable to another, or pass a structure instance
to a procedure argument, the current values of all the variable members are
copied to the new structure. When you assign one object variable to another, or
pass an object variable to a procedure, only the reference pointer is copied.
You can assign the value Nothing to a structure variable, but the instance
continues to be associated with the variable. You can still call its methods and
access its data members, although variable members are reinitialized by the
assignment. In contrast, if you set an object variable to Nothing, you dissociate
it from any class instance, and you cannot access any members through the
variable until you assign another instance to it.
An object variable can have different class instances assigned to it at different
times, and several object variables can refer to the same class instance at the
same time. Changes you make to the values of class members affect those members
when accessed through another variable pointing to the same instance. Structure
members, however, are isolated within their own instance. Changes to their values
are not reflected in any other structure variables, even in other instances of
the same Structure declaration.
Equality testing of two structures must be performed with a member-by-member
test. Two object variables can be compared using the Equals method. Equals
indicates whether the two variables point to the same instance.
"Chris" <ch********@pandora.be> wrote in message
news:5v***********************@phobos.telenet-ops.be...
Hi,

I don't get the difference between a struct and a class !

ok, I know that a struct is a value type, the other a reference type, I
understand the technical differences between both, but conceptually speaking
: when do I define something as 'struct' and when as 'class' ?

for example : if I want to represent a 'Time' thing, containing :
- data members : hours, mins, secs
- some constructors
- some operator overloading functions maybe

do it define it as a struct or as a class ? and why ?

many thanks
Chris

Nov 16 '05 #5
Then again, this is a C# group w00ps...the documentation I posted is for VB7
structures and classes...sorry.

Mythran

"Mythran" <ki********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Some things about structs vs classes I haven't seen mentioned prior to me
"starting" this post:

Pulled from
"ms-help://MS.VSCC/MS.MSDNQTR.2003FEB.1033/vbcn7/html/vaconstructuresandclasses.h tm"

Similarities
Structures and classes are similar in the following respects:

*Both are container types, meaning that they contain other types as members. *Both have members, which can include constructors, methods, properties,
fields, constants, enumerations, events, and event handlers.
*Members of both can have individualized accessibilities. For example, one
member can be declared Public and another Private.
*Both can implement interfaces.
*Both can have shared constructors, with or without parameters.
*Both can expose a default property, provided that property takes at least
one argument.
*Both can declare and raise events, and both can declare delegates.
Differences
Structures and classes differ in the following particulars:

*Structures are value types; classes are reference types.
*Structures use stack allocation; classes use heap allocation.
*All structure members are Public by default; class variables and constants
are Private by default, while other class members are Public by default. This
behavior for class members provides compatibility with the Visual Basic 6.0
system of defaults.
*A structure must have at least one nonshared variable or event member; a
class can be completely empty.
*Structure members cannot be declared as Protected; class members can.
*A structure procedure can handle events only if it is a Shared Sub
procedure, and only by means of the AddHandler statement; any class procedure can handle events, using either the Handles keyword or the AddHandler statement.
*Structure variable declarations cannot specify initializers, the New
keyword, or initial sizes for arrays; class variable declarations can.
*Structures implicitly inherit from the ValueType class and cannot inherit
from any other type; classes can inherit from any class or classes other than
ValueType.
*Structures are not inheritable; classes are.
*Structures are never terminated, so the common language runtime (CLR) never calls the Finalize method on any structure; classes are terminated by the garbage collector, which calls Finalize on a class when it detects there are no active
references remaining.
*A structure does not require a constructor; a class does.
*Structures can have nonshared constructors only if they take parameters;
classes can have them with or without parameters.
*Every structure has an implicit public constructor without parameters. This constructor initializes all the structure's data members to their default values. You cannot redefine this behavior.

Instances and Variables
Because structures are value types, each structure variable is permanently bound to an individual structure instance. But classes are reference types, and an
object variable can refer to various class instances. This distinction affects
your usage of structures and classes in the following ways:

A structure variable implicitly includes an initialization of the members using
the structure's parameterless constructor. Therefore, Dim S As Struct1 is
equivalent to Dim S As Struct1 = New Struct1().
When you assign one structure variable to another, or pass a structure instance
to a procedure argument, the current values of all the variable members are
copied to the new structure. When you assign one object variable to another, or
pass an object variable to a procedure, only the reference pointer is copied.
You can assign the value Nothing to a structure variable, but the instance
continues to be associated with the variable. You can still call its methods and access its data members, although variable members are reinitialized by the
assignment. In contrast, if you set an object variable to Nothing, you dissociate it from any class instance, and you cannot access any members through the
variable until you assign another instance to it.
An object variable can have different class instances assigned to it at different times, and several object variables can refer to the same class instance at the
same time. Changes you make to the values of class members affect those members
when accessed through another variable pointing to the same instance. Structure
members, however, are isolated within their own instance. Changes to their values are not reflected in any other structure variables, even in other instances of
the same Structure declaration.
Equality testing of two structures must be performed with a member-by-member
test. Two object variables can be compared using the Equals method. Equals
indicates whether the two variables point to the same instance.
"Chris" <ch********@pandora.be> wrote in message
news:5v***********************@phobos.telenet-ops.be...
Hi,

I don't get the difference between a struct and a class !

ok, I know that a struct is a value type, the other a reference type, I
understand the technical differences between both, but conceptually speaking
: when do I define something as 'struct' and when as 'class' ?

for example : if I want to represent a 'Time' thing, containing :
- data members : hours, mins, secs
- some constructors
- some operator overloading functions maybe

do it define it as a struct or as a class ? and why ?

many thanks
Chris


Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Daniel Ladd | last post by:
Hi, I have a problem with a conceptual graph in c++. I have a oist of structures like this: typedef struct Conceptual { char* Name;//Rappresenta la parola da mettere nel grafo Conceptual* Next;...
11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little...
3
by: Robben | last post by:
Hi, Part of the question could be OT, so please bare with me for that. As I was told/know/read the main difference between struct and class is that for a struct all member variables and methods...
22
by: Ook | last post by:
We have had a discussion on the differences between a class and a structure, and no one is in agreement. As I understand it, a structure defaults to public, a class defaults to private. There are...
6
by: XiongBin | last post by:
anybody who tell me: what is the difference between "struct" and "class"? :-)
5
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
7
by: raghunandan_1081 | last post by:
Hi guys, can you please tell me what is the Difference between c structure and c++ structure
6
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
6
by: fcvcnet | last post by:
Hi, I read the book C++ Primer, Fourth Edition By Stanley B. Lippman, Jos¨¦e Lajoie, Barbara E. Moo "If we define a class using the class keyword, then any members defined before the first...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.