Struct vs Classes?

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


In C++ it all comes down to how it was allocated. If it was new'd, it's on the heap, otherwise it's on the stack. The same is equally true for classes.

For example:

MyStruct * struct = new MyStruct(); // Heap
MyStruct struct2; // Stack




Yes, exactly as C (using malloc instead of new), but the way you said it seemed like you meant that structs would be allocated on the heap on CPP and on stack on C#.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

Advertisement
Ahh I see. but what frob says brings up another question for me about structs/classes.


If you intend it to hold plain old data and not functions and such, call it a struct.

so does this mean structs can not hold functions or is it just preferred to put functions in classes and plain data in structs?
They can hold functions. Some people just differentiate between structs and classes if they have functions or not (what I commented on earlier; if you need to interact with them). There really is no difference other than default method and inheritance access as far as the language goes.
It's interesting that the convention of struct for really simple things and class for more complex things is actually in the language of the C++ standard itself. For example, most of the time the kinds of classes are just called classes (e.g. "abstract classes" or "base classes") but plain old data classes are called "POD structs" in the standard. It may just be a convention, but it's deeply embedded in the culture of C++ developers. It gets more fun in the C++11 standard when you've got two special kinds of classes: trivially copyable classes and standard layout classes, and if you've got a class that meets both criteria (and a few more on top) it's called a POD struct.
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.

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.


Rather than spouting incorrect information, please note Brother Bob's reply, which is 100% correct:


The only difference is default member and inheritance access. Structs default to public members and inheritance while classes default to private.

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.

I did see Bob's reply. The OP kept posting, and seemed to not understand why someone would use one or the other.

They can hold functions. Some people just differentiate between structs and classes if they have functions or not


That's what I do, and what most companies I've worked for do. I only break the "no member functions" rule by providing my structs with an inline default constructor that initialise the thing to zero (or other meaningful values).

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni


Rather than spouting incorrect information, please note Brother Bob's reply, which is 100% correct:


Once Serapth had thought to ask which language the OP was asking about, sure.... mellow.png It's also far from a full answer, especially for the OP's apparent level of experience.

While not scrupulously correct, I suspect Daark's answer will be more useful to the OP than many of the other contributions to this thread, because it talks about how the concepts are actually used. I agree that Daark's reply contains inaccuracies, but I think it would have been much more helpful of you as a moderator to correct them point by point - thereby continuing the discussion rather than limiting it to the shortest standardese quote possible.
[size="1"]

Once Serapth had thought to ask which language the OP was asking about, sure....

To be clear, the specific language was given from the very beginning of the thread. Not in the most obvious place though; the thread is tagged as "C++".

This topic is closed to new replies.

Advertisement