Jump to content

  • Log In with Google      Sign In   
  • Create Account


Struct vs Classes?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
31 replies to this topic

#1 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 22 October 2012 - 12:32 PM

What is the difference between structs and classes in C++?

Edited by jbadams, 23 October 2012 - 06:26 AM.
: altered the language of the post slightly to clarify the question, as a few users seemed slightly unsure.


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7774

Like
10Likes
Like

Posted 22 October 2012 - 12:40 PM

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

#3 Waterlimon   Crossbones+   -  Reputation: 2350

Like
0Likes
Like

Posted 22 October 2012 - 12:43 PM

You should just google it to find a full comparison, but AFAIK members of classes are private by default while for structs they are public.

For variables at least. Not sure about differences regarding methods abd such.

Waterlimon (imagine this is handwritten please)


#4 Serapth   Crossbones+   -  Reputation: 5178

Like
4Likes
Like

Posted 22 October 2012 - 12:52 PM

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


This.

The only difference is that structs default to public while class defaults to private. This behavior of defaulting to public can be handy, such as with functors, but that is a problem outside of your current paygrade.

The other key difference is, structs exist in C, classes do not.



One thing that is of extreme importance to realize is... this is true ONLY for C++. In C#, a struct is MASSIVELY different than a class and the distinction is of great importance to be aware of, and is something that catches C++->C# programmers off guard. In C#, a struct is allocated on the stack.

Edited by Serapth, 22 October 2012 - 12:53 PM.


#5 KnolanCross   Members   -  Reputation: 1158

Like
-4Likes
Like

Posted 22 October 2012 - 01:45 PM


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


This.

The only difference is that structs default to public while class defaults to private. This behavior of defaulting to public can be handy, such as with functors, but that is a problem outside of your current paygrade.

The other key difference is, structs exist in C, classes do not.



One thing that is of extreme importance to realize is... this is true ONLY for C++. In C#, a struct is MASSIVELY different than a class and the distinction is of great importance to be aware of, and is something that catches C++->C# programmers off guard. In C#, a struct is allocated on the stack.


Can't say for CPP but on C a struct variable is also allocated on the stack (an struct pointer may point to a struct allocated on the stack or on the heap).

EDIT:
Just tested on CPP, also on stack.

Edited by KnolanCross, 22 October 2012 - 01:50 PM.

My blog on programming and games.
http://16bitsflag.blogspot.com.br/

#6 Washu   Senior Moderators   -  Reputation: 4459

Like
6Likes
Like

Posted 22 October 2012 - 01:49 PM

In C#, a struct is allocated on the stack.

No. In C# a struct is a value type. Versus a reference type. The "allocated on the stack" part is merely a consequence of it being a value type and only applies to instances declared within a method or passed via parameters. Your REFERENCE to a class, within a method, is ALSO allocated on the stack.

As for the OP, since the post is tagged with C++, Brother Bob's response sums it up.

Edited by Washu, 22 October 2012 - 01:51 PM.

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.
ScapeCode - Blog | SlimDX


#7 Serapth   Crossbones+   -  Reputation: 5178

Like
3Likes
Like

Posted 22 October 2012 - 02:25 PM

Can't say for CPP but on C a struct variable is also allocated on the stack (an struct pointer may point to a struct allocated on the stack or on the heap).

EDIT:
Just tested on CPP, also on stack.


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


In C#, a struct is allocated on the stack.

No. In C# a struct is a value type. Versus a reference type. The "allocated on the stack" part is merely a consequence of it being a value type and only applies to instances declared within a method or passed via parameters. Your REFERENCE to a class, within a method, is ALSO allocated on the stack.


True, although what I said was not false. It was however increasingly off topic. The most important take away is, the relationship between classes and structs is much more profound in C# than C++.

Edited by jbadams, 23 October 2012 - 06:20 AM.
Fixed quote tag referencing incorrect user.


#8 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 22 October 2012 - 02:57 PM

getting back on topic of C++, so I suppose it is just personal preference since either can be used for same thing? Is their any that is more preferred by advanced C++ programmers, to get me into good habits?

#9 frob   Moderators   -  Reputation: 18831

Like
3Likes
Like

Posted 22 October 2012 - 03:06 PM

getting back on topic of C++, so I suppose it is just personal preference since either can be used for same thing? Is their any that is more preferred by advanced C++ programmers, to get me into good habits?

It is generally a signal of intent.

If you intend it to hold plain old data and not functions and such, call it a struct.
Check out my personal indie blog at bryanwagstaff.com.

#10 Brother Bob   Moderators   -  Reputation: 7774

Like
2Likes
Like

Posted 22 October 2012 - 03:08 PM

It is entirely a personal preference which one you chose. A common way is to use struct for pure data containers and class for objects which has some way of interacting with them. For example, a header for a file format could be a struct since its only purpose is to represent and group the data in a file, but your linked list could be a class since you interact with it by adding data to it. Whatever you decide to do though, consistency is important in any design decision.

Edited by Brother Bob, 22 October 2012 - 03:09 PM.


#11 KnolanCross   Members   -  Reputation: 1158

Like
0Likes
Like

Posted 22 October 2012 - 03:37 PM

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#.
My blog on programming and games.
http://16bitsflag.blogspot.com.br/

#12 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 22 October 2012 - 04:23 PM

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?

#13 Brother Bob   Moderators   -  Reputation: 7774

Like
0Likes
Like

Posted 22 October 2012 - 04:30 PM

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.

#14 SiCrane   Moderators   -  Reputation: 9386

Like
1Likes
Like

Posted 22 October 2012 - 08:29 PM

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.

#15 MrDaaark   Members   -  Reputation: 3539

Like
-3Likes
Like

Posted 22 October 2012 - 09:55 PM

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.

#16 Washu   Senior Moderators   -  Reputation: 4459

Like
2Likes
Like

Posted 22 October 2012 - 11:37 PM

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.


Edited by Washu, 22 October 2012 - 11:37 PM.

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.
ScapeCode - Blog | SlimDX


#17 MrDaaark   Members   -  Reputation: 3539

Like
1Likes
Like

Posted 22 October 2012 - 11:55 PM

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

#18 kunos   Crossbones+   -  Reputation: 2183

Like
0Likes
Like

Posted 22 October 2012 - 11:58 PM

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
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#19 mrbastard   Members   -  Reputation: 1573

Like
0Likes
Like

Posted 23 October 2012 - 05:12 AM

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.... Posted Image 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.

Edited by mrbastard, 23 October 2012 - 05:51 AM.



#20 Brother Bob   Moderators   -  Reputation: 7774

Like
1Likes
Like

Posted 23 October 2012 - 06:07 AM

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++".




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS