Struct vs Classes?

Started by
30 comments, last by jbadams 11 years, 5 months ago
What is the difference between structs and classes in C++?
Advertisement
The only difference is default member and inheritance access. Structs default to public members and inheritance while classes default to private.
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.

o3o


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.

[quote name='Brother Bob' timestamp='1350931254' post='4992846']
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.
[/quote]

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.

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


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.

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.


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


[quote name='Serapth' timestamp='1350931978' post='4992852']
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.
[/quote]

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

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

This topic is closed to new replies.

Advertisement