Game Programming and Development Tools

Structures and Classes – Wacko4X

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.)
A class has features such as inheritance, and thus different children can add, overide both the data and the code, a structure is static, and doesn't have inheritance..

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

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

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 i class could have code like 'validatestate' or zip, maybe add field - like country, or add a class that can deal with canadian addresses etc.

but in a sense a class is a structure with code added, but more features..
you can simulate a class say in standard C with a structure, with some of the structures members being function pointers or such.

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

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:


struct MyStruct
{
int a; // a is *public*
private:
int b: // b is private
public:
int c; // c is public
};

class MyClass
{
int a; // a is *private*
private:
int b; // b is private
public
int c; // c is public
};

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:


struct MyStruct
{
private:
int a, b, c;
protected:
float x, y, z;
float total;
public:
MyStruct(); // default constructor
MyStruct(int init_a, int init_b, int init_c); // initializing constuctor

void SetValues(float set_x, float set_y, float set_z);
inline float GetTotal() { total = x+y+z; return total; };
};

struct MyClass
{
private:
int a, b, c;
protected:
float x, y, z;
float total;
public:
MyClass(); // default constructor
MyClass(int init_a, int init_b, int init_c); // initializing constuctor

void SetValues(float set_x, float set_y, float set_z);
inline float GetTotal() { total = x+y+z; return total; };
};

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).

------------------
Brian