473,795 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why are some types implemented as struct?

I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? Why is BitArray
implemented as a class, but BitVector32 a struct?

In general, in OO design, how do we determine if we should use struct
or class?
Jul 28 '08 #1
12 1562
MC
>I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? Why is BitArray
implemented as a class, but BitVector32 a struct?

In general, in OO design, how do we determine if we should use struct
or class?
I think that, in general, structs are for small things that use a fixed
amount of memory.
Jul 28 '08 #2
On Jul 28, 10:33*am, Author <gnewsgr...@gma il.comwrote:
I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? *Why is BitArray
implemented as a class, but BitVector32 a struct?

In general, in OO design, how do we determine if we should use struct
or class?
Struct are placed in memory on the stack, therefore faster and do not
need to be cleaned up with garbage collection. Structs cannot be
extended and cannot extend anything else.

Classes Are on the heap, must be cleaned up and can be extended or
extend.

For most of us, a big "So what?"

I am assuming the choice af struct vs class is something that MS
thinks a lot more about in a framework than the common code grunt and
it comes down to first, performance and then to Symmetry in the code.

thats my take on it.

Tal
Jul 28 '08 #3
Nearly every time I have designed something as a struct I have later
discovered I need to be able to change the values in it and ended up
converting it to a class, so now I just don't bother with them any more.

Jul 28 '08 #4
"Peter Morris" <mr*********@SP AMgmail.comwrot e in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
Nearly every time I have designed something as a struct I have later
discovered I need to be able to change the values in it and ended up
converting it to a class, so now I just don't bother with them any more.
You can change values in a struct as easily as you can in a class. In fact,
in a lot of ways, you can make a struct act like a class. You can create
properties that you can get and set, just like a class. You can create
methods, you can make field members public. So I'm not really sure what you
mean.
Jul 28 '08 #5
On Jul 28, 12:23*pm, "MC" <for.address.l. ..@www.ai.uga.e du.slash.mc>
wrote:
I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? *Why is BitArray
implemented as a class, but BitVector32 a struct?
In general, in OO design, how do we determine if we should use struct
or class?

I think that, in general, structs are for small things that use a fixed
amount of memory.
Hmm, sounds reasonable, at least with regard to BitArray (resizable)
and BitVector32 (not resizable). But is this the only possible reason
that some types are implemented as struct and others class?

I've read the other responses of my question, but they seem to be off-
topic. I would like to hear some guruish comment about my question.
Jul 28 '08 #6
On Mon, 28 Jul 2008 11:53:40 -0700, Fredo <fr***@hotmail. comwrote:
"Peter Morris" <mr*********@SP AMgmail.comwrot e in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>Nearly every time I have designed something as a struct I have later
discovered I need to be able to change the values in it and ended up
converting it to a class, so now I just don't bother with them any more.

You can change values in a struct as easily as you can in a class.
Well, yes and no.

There are some serious problems with making a struct mutable. The fact
that structs are _copied_ rather than references leads to code that either
doesn't work as expected (you think you've changed an instance but have
only changed a copy of an instance), or just doesn't compile, or is more
inconvenient than it could or should be.

Pete
Jul 28 '08 #7
On Mon, 28 Jul 2008 11:12:39 -0700, tal_mcmahon <ta*********@ho tmail.com>
wrote:
Struct are placed in memory on the stack,
No. This is a common, but serious misconception.

Structs are "value types". That means that the storage for the type is
allocated where the variable itself is stored. But only local variables
wind up on the stack. A struct used in a class, or in an array, or
anything else that is itself allocated on the heap will wind up on the
heap itself.
therefore faster and do not
need to be cleaned up with garbage collection.
Structs need to be collected when boxed. They also wind up collected
implicitly when they are part of a class that is collected.

In fact, if a struct winds up boxed on a regular basis that will suggest
that the data structure should have been a class after all.

Pete
Jul 28 '08 #8

"Author" <gn********@gma il.comwrote in message
news:00******** *************** ***********@f63 g2000hsf.google groups.com...
On Jul 28, 12:23 pm, "MC" <for.address.l. ..@www.ai.uga.e du.slash.mc>
wrote:
I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? Why is BitArray
implemented as a class, but BitVector32 a struct?
In general, in OO design, how do we determine if we should use struct
or class?

I think that, in general, structs are for small things that use a fixed
amount of memory.
Hmm, sounds reasonable, at least with regard to BitArray (resizable)
and BitVector32 (not resizable). But is this the only possible reason
that some types are implemented as struct and others class?

I've read the other responses of my question, but they seem to be off-
topic. I would like to hear some guruish comment about my question.

*************** **************
I'm only a beginner myself, but nobody has mentioned that classes are
inheritable, whereas structs are not. If you are looking at this from an OOP
perspective, this is a huge difference.
Jul 29 '08 #9
To be honest I don't even remember the problems I had, I just remember that
every time I have used them I have ended up switching to classes so now I
don't bother.
Pete

Jul 29 '08 #10

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

Similar topics

1
2002
by: Guilherme Pinto | last post by:
Hello. I am reading the book written by Bjarne Stroustrup called " The C++ Programming Language - Special Edition" and had a doubt which a think is really important to distinguish between the main features of modules, namespaces, and User-Defined types. The text above was copied from page 31. --------------------------------------------------------
193
9652
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
11
3193
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to accomplish it. For example: int main void() { while there are more data types { print next data type; }
48
2182
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
3
2675
by: Bear | last post by:
Hello there How come it's possible to add values of the type "int" into an ArrayList when the Add member function only accepts values of type "object"? Is an int just a "typedef" of the Int32 structure and, if so, does the runtime system load a value type onto the stack even though it only needs to load a primitive type? -- Best regards
1
1782
by: albert_reade | last post by:
Hello I was wondering if someone could please help me understand what I need to do in order to get this project to work. I just need some hints or a push in the right direction to get this to work, thanks. Design the Array of Expected Events, AEE, required by the interrupt system. Each entry i, 0 <= i < 6, in this array contains three fields, AEE.IIC, AEE.InterruptHandler, AEE.WaitingQueue. These elds are designed such that they can...
19
1303
by: Marco Trapanese | last post by:
Hello, I need some help to understand what I'm doing :) I'm writing code for an embedded system and I need to create a navigable menu system. I'm going to use these structures: typedef struct { char *name; int type;
2
2135
by: Stefan Hoffmann | last post by:
hi @all, has anybody an explanation, why string? is not allowed for an auto-implemented property? namespace Test { class Class1 { int? NullableInt { get; set; }
50
4519
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.