Struct vs Classes?

Started by
30 comments, last by jbadams 11 years, 6 months ago

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

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.


I wouldn't say random. The default constructor is pretty well documented in the standard.

That leads to an interesting ( and yes, related... at least to KaiserJohan's comment ) trivia question ( and one of those C++ hand grenades in waiting ).

What will this program output and why?

#include <iostream>
int main(int agrc, char** argv)
{
int* meaningOfLife = new int;
int* meaningOfLife2 = new int();
std::cout << *meaningOfLife << " and " << *meaningOfLife2 << std::endl;
}

[quote name='Serapth' timestamp='1350999819' post='4993085']
What will this program output and why?

#include <iostream>
int main(int agrc, char** argv)
{
int* meaningOfLife = new int;
int* meaningOfLife2 = new int();
std::cout << *meaningOfLife << " and " << *meaningOfLife2 << std::endl;
}




NULL and NULL

Nether pointer are pointing to anything.
[/quote]

Nope, not the answer. Here's a hint, one is a pointer to something and technically neither will return null.
The first one will print an undefined value since the integer allocated by new int is not initialized. The second one will print zero, because the integer allocated by new int() is default initialized to zero. The parentheses syntax default initializes primitive types to zero. The same goes for members; if you explicitly default initialize a primitive member of a structure or a class in the initializer list it is initialized to zero; if you omit it from the initializer list it is left with an undefined value.

The first one will print an undefined value since the integer allocated by new int is not initialized. The second one will print zero, because the integer allocated by new int() is default initialized to zero. The parentheses syntax default initializes primitive types to zero. The same goes for members; if you explicitly default initialize a primitive member of a structure or a class in the initializer list it is initialized to zero; if you omit it from the initializer list it is left with an undefined value.


This man gets the prize. Granted, that the prize is nothing makes winning this particular contest bittersweet at best.

[quote name='Brother Bob' timestamp='1351003139' post='4993100']
The first one will print an undefined value since the integer allocated by new int is not initialized. The second one will print zero, because the integer allocated by new int() is default initialized to zero. The parentheses syntax default initializes primitive types to zero. The same goes for members; if you explicitly default initialize a primitive member of a structure or a class in the initializer list it is initialized to zero; if you omit it from the initializer list it is left with an undefined value.


This man gets the prize. Granted, that the prize is nothing makes winning this particular contest bittersweet at best.
[/quote]

Technically he should say unspecified value, slightly different meaning in the context of the standard language. But yes, that is one of the "great" things about C++, something as trivial and difficult to notice in the overall context of a large set of code can have a massive difference in the behavior of the application as a whole. Such as () vs no () when allocating an object or array of objects. Its even worse in the array case:


#include <iostream>
int main() {
int* p1 = new int[10];
int* p2 = new int[10]();
for(int i = 0; i < 10; ++i) {
std::cout<<p1<<std::endl;
std::cout<<p2<<std::endl;
}
delete [] p1;
delete [] p2;
}


Which not only looks terrible, but has additional symbols just to make it even more confusing. Ahh, the joys of C++ smile.png

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Listen to KaiserJohan. He knows what he's talking about, however, he defined a 32BPP pixel incorrectly with alpha last in rightmost byte (0xAABBCC*DD*). It should be like this:

#define byte unsigned char

typedef struct {
byte a, r, g, b;
} PIXEL;

You must learn what your code converts to. My ASM programming site: http://sungod777.zxq.net/

You must learn what your code converts to. My ASM programming site: http://sungod777.zxq.net/


No, you mustn't. In fact, you should very much do the exact opposite of this, especially when starting out.

To the OP, if I hear you are learning assembly as a first language, I am going to hunt you down and beat you with a dead mackerel.

Besides, even your example doesn't guarantee the byte order within the struct. The compiler is perfectly within it's rights and in fact, probably will, byte pad that value to get the ideal alignment.

From the C++ 03 standard:
Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

This is completely ignoring the fact "rightmost" byte is completely relative to the system you are running on. If you insist on giving beginners bad advice, please at least make it factually correct bad advice.
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.
Yes. But he was asking about usage cases of one or the other in his follow up posts. They are a hold over for C style backwards compatibility, and they get used as such.

This topic is closed to new replies.

Advertisement