Wacko4X![]() Member Posts: 92 From: Bellvue, WA, USA Registered: 08-21-2002 |
Due to D-Sipl's (a poster who I highly respect) last thread, I decided to ask some questions that I have had a lot of trouble grasping so here goes the first question. What is a structure and how is it different from a class? What languages use structures/classes? And in what are some of the postitive and negative effects of them? (in the areas of readability, concept understanding, code optomization - written and compiled - etc.) Thanks guys! |
||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
it all depends on the language.. typically a structure is a plain structure, meaning a group of structure data, and resides in memory. a structure Does not have any code.. while a class contains Code and Data, also a structure is typically static, once it is defined it is defined, and does not change (however certian language support structures, that you subparts of it can represent different data.) However in C#, a structure can have code in it most object orientated languages would have both structures and classes such as C++,Delphi, C#, java and pigsnose, VB.net Most older languages still have structures, even a good assembler has structures.. so basic, c, pascal, mostly anything. Karl ------------------ |
||
Wacko4X![]() Member Posts: 92 From: Bellvue, WA, USA Registered: 08-21-2002 |
I thought that if you wrote a structure into your code but never created an instance of it than nothing would be loaded into memory (in terms of that structure). Also, structures hold only variables but cant those be changed? and arnt classes basically structures with functions/methods? |
||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
yeah, unless you have an instance of a structure, it isn't in memory, when i mean its static, i mean if you say have a structure for an address with name, addressline, city, state, zip it will always be that.. and only store that info, and always use the same amount of memory (unless of course the members of the structure (variables) are just pointers to a more dynamic type.. but still the structure itself will only use the same amount of memory. and also there is no code.. but in a sense a class is a structure with code added, but more features.. ------------------ |
||
Wacko4X![]() Member Posts: 92 From: Bellvue, WA, USA Registered: 08-21-2002 |
Thats some cool beans, but are there any benefits past having all of your information in one place? What about the negative aspects (other than structures being static?) |
||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
I don't know about other languages, but in C++ (and thus I suspect C# as well), there is only one difference between structs and classes: structs default to public members, while classes default to private members:
In C++, that's the ONLY difference. If you don't explicity create constructors/destructors for your classes and structs, the compiler will create them for you anyway. Note this is only for C++. In C, there are no classes, and structs cannot have member functions including constructors/destructors. As such, for simply historic/habit reasons, people generally don't put member functions in structs, and use classes when they want member functions. But the language allows you to treat structs and classes identically. This struct and this class are both completely valid and completely identical:
There's really no downside to using structs/classes. The only thing I can think of is that you should be aware that in C++, if you don't explicity declare constructors/destructors, the compiler adds them anyway. This, along with alignment settings for the compiler, may result in their sizes and addresses (in an array or whatever) not being obvious (i.e. don't assume/hardcode sizes or addresses, which you shouldn't do anyway). |