473,789 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

design question : struct or class

Hello,

I've got a design question. I need to keep track of some variables and I am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

Since there's no real business logic my choice would be structs. But looking
at the size of the objects in memory I would think they are a bit large to
put on the stack and should be better of on the heap. Which would mean I
should use classes.

Any ideas about what would be best?

TIA

Yves
Nov 15 '05 #1
26 1940
Phoenix,
Since there's no real business logic my choice would be structs. Structs can have "business logic" as equally well as classes.

I normally make every thing a class, unless I know I need a struct, I use
the following to know I need a struct:
- act like a primitive type
- have an instance size under 16 bytes
- are immutable
- value semantics are desirable

http://msdn.microsoft.com/library/de...Guidelines.asp

Hope this helps
Jay

"phoenix" <pa******@skyne tWORK.be> wrote in message
news:O1******** ******@TK2MSFTN GP12.phx.gbl... Hello,

I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

Since there's no real business logic my choice would be structs. But looking at the size of the objects in memory I would think they are a bit large to
put on the stack and should be better of on the heap. Which would mean I
should use classes.

Any ideas about what would be best?

TIA

Yves

Nov 15 '05 #2
It sounds like your object has the potential of being quite large. Remember
that structures are allocated on the stack, which is finite. I don't know
the stack size off the top of my head.

"phoenix" <pa******@skyne tWORK.be> wrote in message
news:O1******** ******@TK2MSFTN GP12.phx.gbl...
Hello,

I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

Since there's no real business logic my choice would be structs. But looking at the size of the objects in memory I would think they are a bit large to
put on the stack and should be better of on the heap. Which would mean I
should use classes.

Any ideas about what would be best?

TIA

Yves

Nov 15 '05 #3
Pheonix... To be clear, if you add structures to a .NET _collection_,
they
will be boxed anyway.

Regards,
Jeff
array of max 255 of these classes/structs (in most cases it will be

less
then 5 however)<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
Peter... To be clear, structures can be inlined if they are part of a
class or
?array.

Regards,
Jeff
Remember that structures are allocated on the stack, which is finite.<


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #5
> I've got a design question. I need to keep track of some variables and I
am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)


You should *Always* use classes. structs are only needed in very rare
circumatances which isn't the case.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #6
On Fri, 30 Jan 2004 00:56:36 +0100, "codymanix"
<do************ *********@gmx.d e> wrote:
I've got a design question. I need to keep track of some variables and I

am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would

depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)


You should *Always* use classes. structs are only needed in very rare
circumatance s which isn't the case.


Rubbish....anyo ne saying "always" do or don't do X is practicing
religion, not programming. Structs have their proper place in the
programming paradigm, just as classes do. Knowing which to use and
when defines the difference between a programmer and a zealot/ostrich.

Oz

Nov 15 '05 #7
Use them whatever best way suits you. Scew the fanboys.

Same for any aspect of software. Do what works best for you.
"ozbear" <oz*****@yahoo. com> wrote in message
news:401a31e1.1 431256578@news-server...
On Fri, 30 Jan 2004 00:56:36 +0100, "codymanix"
<do************ *********@gmx.d e> wrote:
I've got a design question. I need to keep track of some variables and I
am
planning to put them inside a class or struct. Basically I'm talking

about 10 bools, 20 ints and 2 arrays of ints. The size of the arrays would

depend
on some external value (going from 0 to around 1000 max). I would have an array of max 255 of these classes/structs (in most cases it will be less then 5 however)


You should *Always* use classes. structs are only needed in very rare
circumatance s which isn't the case.


Rubbish....anyo ne saying "always" do or don't do X is practicing
religion, not programming. Structs have their proper place in the
programming paradigm, just as classes do. Knowing which to use and
when defines the difference between a programmer and a zealot/ostrich.

Oz

Nov 15 '05 #8
Hi there,

The main difference is that a 'class' is an object type and a struct is a
'value' type. When it comes to assignment and parameter passing its
important to know the difference:
Assignment
-------------
myObject o1 = new myObject()
myObject o2 = new myObject()
o1 = o2

Objects are 'reference' types and are really pointers to an address in
memory where values are stored. In the above example space is allocated for
two myObject types. o1 points to the first myObject type and o2 points to
the second myObjects type. Because objects are reference types, its not the
values held in o2 that are copied into o1 but rather the address in memory
that is pointed too by o2. Consequently, 'o1 = o2' has the effect of
pointing both o1 and o2 at the same place in memory and changes to o1 will
change o2 and vice versa. If no references remain to the memory allocated
for the original o1 object it will eventually be garbage collected.

myStruct s1 = new myStruct()
myStruct s2 = new myStruct()
s1 = s2

Structs are 'value' types and are very much like ints or floats. In the
above example space is allocated for s1 and s2. s1 is the first myStruct
type and s2 is the second myStruct type. Because structs are value types,
its not the reference for s2 that is copied into s1 but the values.
Consequently, 's1 = s2' has the effect of maintaining two seperate areas in
memory with the same values. Sometimes this is what you want and sometimes
it isn't. For example, If your struct type contains a nested object it isn't
the values of the nested object that are copied but rather the reference. In
the case of 's1 = s2', you will have two seperate blocks of memory which
contain the same values but the object reference in both will point to the
same place in memory. To find out more about avoiding this problem look for
help and shallow vs deep copying.
Parameter Passing
---------------------

When objects are passed to a method, they are passed by reference. This
means that any changes made to the object within the method will remain once
the method had been completed.

When structs are passed to a method, they are passed by value. This means
that the system makes a copy is made and changes made to the struct will not
remain when the method is complete. You can get round this by returning the
copied struct and assigning it to a variable but there is an obvious over
head in working with copies.
Summary
----------

The choice of whether to use a class or a struct is entirely down to you,
both have there place. However, because of the overheads involved in passing
by value and the added complexity of nested objects, I uses classes most of
the time. I only use structs when the offer me exactly what i need. I hope
this helps and that my explanation was too confusing and if I am wrong on
any point my apologies in advance.
Happy coding, Peter


"codymanix" <do************ *********@gmx.d e> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I've got a design question. I need to keep track of some variables and I

am
planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of ints. The size of the arrays would

depend
on some external value (going from 0 to around 1000 max). I would have an array of max 255 of these classes/structs (in most cases it will be less
then 5 however)


You should *Always* use classes. structs are only needed in very rare
circumatances which isn't the case.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Nov 15 '05 #9
Hi there,

The main difference is that a 'class' is an object type and a struct is a
'value' type. When it comes to assignment and parameter passing its
important to know the difference:
Assignment
-------------
myObject o1 = new myObject()
myObject o2 = new myObject()
o1 = o2

Objects are 'reference' types and are really pointers to an address in
memory where values are stored. In the above example space is allocated for
two myObject types. o1 points to the first myObject type and o2 points to
the second myObjects type. Because objects are reference types, its not the
values held in o2 that are copied into o1 but rather the address in memory
that is pointed too by o2. Consequently, 'o1 = o2' has the effect of
pointing both o1 and o2 at the same place in memory and changes to o1 will
change o2 and vice versa. If no references remain to the memory allocated
for the original o1 object it will eventually be garbage collected.

myStruct s1 = new myStruct()
myStruct s2 = new myStruct()
s1 = s2

Structs are 'value' types and are very much like ints or floats. In the
above example space is allocated for s1 and s2. s1 is the first myStruct
type and s2 is the second myStruct type. Because structs are value types,
its not the reference for s2 that is copied into s1 but the values.
Consequently, 's1 = s2' has the effect of maintaining two seperate areas in
memory with the same values. Sometimes this is what you want and sometimes
it isn't. For example, If your struct type contains a nested object it isn't
the values of the nested object that are copied but rather the reference. In
the case of 's1 = s2', you will have two seperate blocks of memory which
contain the same values but the object reference in both will point to the
same place in memory. To find out more about avoiding this problem look for
help and shallow vs deep copying.
Parameter Passing
---------------------

When objects are passed to a method, they are passed by reference. This
means that any changes made to the object within the method will remain once
the method had been completed.

When structs are passed to a method, they are passed by value. This means
that the system makes a copy is made and changes made to the struct will not
remain when the method is complete. You can get round this by returning the
copied struct and assigning it to a variable but there is an obvious over
head in working with copies.
Summary
----------

The choice of whether to use a class or a struct is entirely down to you,
both have there place. However, because of the overheads involved in passing
by value and the added complexity of nested objects, I uses classes most of
the time. I only use structs when the offer me exactly what i need. I hope
this helps and that my explanation was too confusing and if I am wrong on
any point my apologies in advance.
Happy coding, Peter


"codymanix" <do************ *********@gmx.d e> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I've got a design question. I need to keep track of some variables and I

am
planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of ints. The size of the arrays would

depend
on some external value (going from 0 to around 1000 max). I would have an array of max 255 of these classes/structs (in most cases it will be less
then 5 however)


You should *Always* use classes. structs are only needed in very rare
circumatances which isn't the case.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Nov 15 '05 #10

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

Similar topics

8
3772
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various events. The callbacks specified by the subscriber will be called when the events get trigerred in a different thread. Each event has different kinds of data associated with it. To achieve this I have the following: // The following describes the...
11
1428
by: | last post by:
I have a rather newbie question regarding design of class hierarchy. Suppose I have a class Class0, and need to implement a public Class0.compute() interface. There are three different ways to choose the implementation, and the user of the class has to be able to make that choice. Here is what I have in mind. I declare three classes, ClassA, ClassB, ClassC, and each of them has a distinct compute() method that can be used by Class0. ...
31
2000
by: grahamo | last post by:
This came up in an interview I did a while ago and I wanted to know the correct answer. The setup is this; If I have a base class "food" and also two classes "meat" and "veg" that inherit from food, thus; food / \ / \ meat veg
0
1122
by: ma740988 | last post by:
I've provided a stripped down version - as best I could - to get us by. That said, I'm in a quandry with respect to a design here. Consider: // stream.h #ifndef STREAM_H #define STREAM_H # pragma warning (disable : 4786) // if using microsofts compiler ver6
0
1546
by: ma740988 | last post by:
I'm going through modern C++ design looking for tips and while hi-tech I suspect one solution to my issue would involve the factory design pattern. // algorithms.h class Algorithms { protected: typedef std::deque<double> DDEQUE; // need to make this even more generic to support floats .. i.e float and double public:
5
1846
by: aaragon | last post by:
Hello everybody, I appreciate your taking the time to take a look at this example. I need some help to start the design of an application. To that purpose I'm using policy-based design. The idea is to have a Class that stores elements of Class2 in different ways (arrays in stack memory, arrays in heap memory, using the std::vector and so on). I would like the user to customize the creation of a class with Class2 and StoragePolicy like...
3
2489
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
8
1873
by: obrianpatrick | last post by:
Hi, I am relatively new to object oriented programming and design. I am developing an application in VS 2005. I am having the following design problem: I have two interfaces X and Y. Y is derived from X as the following: __interface X {
1
1614
by: mattmao | last post by:
I am brand new to C#.NET so here is my trial on this lab exercise: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace lab02exec { public class Program
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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
10410
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...
0
9020
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
7529
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
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.