Is there any advantage over using a class over a struct in cases such as these? (note: it will only hold variables, there will never be functions)
class Foo {
private:
struct Pos { int x, y, z };
public:
Pos Position;
};
Versus:
struct Foo {
struct Pos { int x, y, z } Pos;
};
Similar questions:
- http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c
- http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c
- http://stackoverflow.com/questions/85553/when-should-i-use-a-struct-instead-of-a-class
-
There is no real advantage of using one over the other, in c++, the only difference between a struct and a class is the default visibility of it's members (structs default to public, classes default to private).
Personally, I tend to prefer structs for POD types and use classes for everything else.
EDIT: litb made a good point in the comment so I'm going to quote him here:
one important other difference is that structs derive from other classes/struct public by default, while classes derive privately by default.
Johannes Schaub - litb : one important other difference is that structs derive from other classes/struct public by default, while classes derive privately by default.From Evan Teran -
"(note: it will only hold variables, there will never be functions)"
Never is a big word. Usually "never" means "eventually". Since that's the case, I'd suggest you use a class. That way, when things change, you don't have so much to change.
The Java (and Python) folks have gotten along fine with everything being a class. It hasn't hurt them any to not have these specialized method-less classes that C++ calls a "struct".
Federico Ramponi : Yes, but there are cases in which you must use C++ compound types with C code, system calls etc To emphasize that a C++ data type is, and will remain, nothing more than C plain old data, I prefer to denote it as a struct. In all the other cases, I use classes.le dorfier : And consequently refactoring it into a class is pretty trivial in the unlikely case that becomes useful. I bet there's a tool in VS to do it for you - and elsewhere as well if you're not using VS.From S.Lott -
The only difference between a class and a struct is that struct members are public by default and class members are private by default. So I say go with whichever one you like best. I'm sure there are arguments to be made in terms of which one is more readable, but I really don't think it's a big deal.
From Jason Baker -
structandclassmean exactly the same thing in C++ with the exception that the default access for struct members and bases is public whereas it is private for classes. I tend to chose struct for classes that only have public members and classes for everything else, but it's only a style issue.From Charles Bailey -
If the contents of the type have no memory allocation issues (such as plain int), then using
structis fine if that's the way you want to go and you've made a conscious decision about it that you can justify to those who use your code. However, if any of the members is a pointer type, then you need to think hard about the memory management issues. It may still be OK to use astruct, but you are much more likely to need a destructor, and some constructors, and so on. At that point, you want aclass.MKroehnert : In C++ it is also possible to add constructors and methods to `struct`s.From Jonathan Leffler -
Essentially the choice between a struct and a class comes down to your style and how much you want to type.
- If you only have public members in a class/struct you might as well use the struct keyword. It will save you having to type out "public:" later on.
- The other reason to choose a struct over a class would be to implicitly document the intent of the object. So you would make POD types structs (even if they contain a constructors and some static helper methods etc), and you would use class for all the other "regular" classes.
From Daemin -
One side point is that structs are often used for aggregate initialized data structures, since all non-static data members must be public anyway (C++03, 8.5.1/1).
struct A { // (valid) { int a; int b; } x = { 1, 2 }; struct A { // (invalid) private: int a; int b; } x = { 1, 2 }; class A { // (invalid) int a; int b; } x = { 1, 2 }; class A { // (valid) public: int a; int b; } x = { 1, 2 }; class A { // (invalid) public: int a; private: int b; } x = { 1, 2 };From Roger Nelson
0 comments:
Post a Comment