Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualKaiserJohan

Posted 23 October 2012 - 06:23 AM

Both are used for the same thing. To group bits of data together to represent 1 thing. Like a pixel, you have red, green, blue, and alpha components.

So a 32 bit pixel would look like this:

Struct Pixel
{
byte R;
byte G;
byte B
byte A;
}

This is just a simple structuring of data, so you can keep track of all the components that come together to represent a pixel. Everything is publicly visible, just like a C style struct. You can access all members like this:

Pixel pxExampleRed;
pxExampleRed.R = 255;
pxExampleRed.B = 0;
pxExampleRed.G = 0;
pxExampleRed.A = 255;

You have to remember to assign a value to all components of your struct, otherwise they will have whatever random value was set in memory where they were allocated.

Sometimes it's nice to fill out a structure to pass to a function, instead of having a giant function call that takes up 20 lines. Windows API programming is a lot like this. You can also use a structure as to return data from a function.

Classes are a bit more complicated. Their data tends to NOT be publicly visible. Instead of accessing their data directly, you call functions that will take your input and do something with it to the object that the class is representing. They also have constructors and destructors so they can handle their own initialization and clean up.

The intent of a struct is a simple grouping of data for convenience. A class is an more complex object that can take care of itself.


Structs have constructors and destructors aswell.. they get 'random' values if you dont define them (i.e the compiler generates them), which is exactly the same for a class.

#1KaiserJohan

Posted 23 October 2012 - 06:22 AM

Both are used for the same thing. To group bits of data together to represent 1 thing. Like a pixel, you have red, green, blue, and alpha components.

So a 32 bit pixel would look like this:

Struct Pixel
{
byte R;
byte G;
byte B
byte A;
}

This is just a simple structuring of data, so you can keep track of all the components that come together to represent a pixel. Everything is publicly visible, just like a C style struct. You can access all members like this:

Pixel pxExampleRed;
pxExampleRed.R = 255;
pxExampleRed.B = 0;
pxExampleRed.G = 0;
pxExampleRed.A = 255;

You have to remember to assign a value to all components of your struct, otherwise they will have whatever random value was set in memory where they were allocated.

Sometimes it's nice to fill out a structure to pass to a function, instead of having a giant function call that takes up 20 lines. Windows API programming is a lot like this. You can also use a structure as to return data from a function.

Classes are a bit more complicated. Their data tends to NOT be publicly visible. Instead of accessing their data directly, you call functions that will take your input and do something with it to the object that the class is representing. They also have constructors and destructors so they can handle their own initialization and clean up.

The intent of a struct is a simple grouping of data for convenience. A class is an more complex object that can take care of itself.


Structs have constructors and destructors aswell.. they get 'random' values if you dont define them, which is exactly the same for a class.

PARTNERS