473,406 Members | 2,847 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,406 software developers and data experts.

Struct or Class

I've always followed a particular standard when considering structs, I
mainly use a struct when a class isn't necessary (ie no requirement for
multiple references).

What is the recommendation when considering structs? What matters and does
it "really" make a big difference?

Thanks
Kev
Jul 10 '06 #1
7 1493
It makes a difference; as you probably know, you must fully initial a
struct by the time the constructor ends, so every field must have a
value. Also, I believe structs are allocated on the stack instead of
the heap. I think structs are 'faster' to use than a class, because
they are not as heavyweight as a class would be. All structs are
implicitly 'sealed' so you can't inherit from them.

Here's a good list: http://www.jaggersoft.com/pubs/StructsVsClasses.htm

Mantorok wrote:
I've always followed a particular standard when considering structs, I
mainly use a struct when a class isn't necessary (ie no requirement for
multiple references).

What is the recommendation when considering structs? What matters and does
it "really" make a big difference?

Thanks
Kev
Jul 10 '06 #2
A bit more details here:
http://msdn2.microsoft.com/en-us/lib...09(d=ide).aspx

chanmm

"Mantorok" <ma******@mantorok.comwrote in message
news:e8**********@newsfeed.th.ifl.net...
I've always followed a particular standard when considering structs, I
mainly use a struct when a class isn't necessary (ie no requirement for
multiple references).

What is the recommendation when considering structs? What matters and does
it "really" make a big difference?

Thanks
Kev

Jul 10 '06 #3
A struct is a value type. Therefore, it should be used whenever the
object it represents acts like a value, that is, if two separate
objects with the value are the same as being the same object.

int a = 5;
int b = a;
a = 6;
// but b remains equal to 5;

Person d = new Person("John Smith");
Person e = d;
d.LastName = "Jones";

Now d = "John Jones". Now, in your case, should e = "John Jones"
(reference type, class), or "John Smith" (value type, struct) ?

Mantorok wrote:
I've always followed a particular standard when considering structs, I
mainly use a struct when a class isn't necessary (ie no requirement for
multiple references).

What is the recommendation when considering structs? What matters and does
it "really" make a big difference?
Jul 10 '06 #4
James nailed it.

This topic comes up here once every few weeks. You can read lots of
back-and-forth about when to use struct. Just search the archives of
this group for "struct vs class" or something like that, and you'll
find lots of spirited discussion.

ja**********@gmail.com wrote:
A struct is a value type. Therefore, it should be used whenever the
object it represents acts like a value, that is, if two separate
objects with the value are the same as being the same object.

int a = 5;
int b = a;
a = 6;
// but b remains equal to 5;

Person d = new Person("John Smith");
Person e = d;
d.LastName = "Jones";

Now d = "John Jones". Now, in your case, should e = "John Jones"
(reference type, class), or "John Smith" (value type, struct) ?

Mantorok wrote:
I've always followed a particular standard when considering structs, I
mainly use a struct when a class isn't necessary (ie no requirement for
multiple references).

What is the recommendation when considering structs? What matters and does
it "really" make a big difference?
Jul 10 '06 #5
Andy <aj*****@alum.rit.eduwrote:
It makes a difference; as you probably know, you must fully initial a
struct by the time the constructor ends, so every field must have a
value. Also, I believe structs are allocated on the stack instead of
the heap.
That's an oversimplification which has caused confusion in the past.
See http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 10 '06 #6
A struct is a simplified class...(waiting for the fire and brimstone :-(

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Andy <aj*****@alum.rit.eduwrote:
>It makes a difference; as you probably know, you must fully initial a
struct by the time the constructor ends, so every field must have a
value. Also, I believe structs are allocated on the stack instead of
the heap.

That's an oversimplification which has caused confusion in the past.
See http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jul 10 '06 #7
Alvin Bruney [MVP] wrote:
A struct is a simplified class...(waiting for the fire and brimstone :-(
"Your mother was a hamster, and your father smelt of elderberries!"

Jul 10 '06 #8

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

Similar topics

21
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
2
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
4
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
5
by: jwright | last post by:
I have decided to use a struct to collect my data. The input file is comma dilineated between almost all of the fields. Here is the code I have so far and a sample input and output file. ...
1
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with...
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.