views:

1433

answers:

7

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:

+14  A: 

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.

Evan Teran
one important other difference is that structs derive from other classes/struct public by default, while classes derive privately by default.
Johannes Schaub - litb
+1  A: 

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

S.Lott
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.
Federico Ramponi
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.
le dorfier
+2  A: 

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.

Jason Baker
+5  A: 

struct and class mean 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.

Charles Bailey
A: 

If the contents of the type have no memory allocation issues (such as plain int), then using struct is 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 a struct, but you are much more likely to need a destructor, and some constructors, and so on. At that point, you want a class.

Jonathan Leffler
In C++ it is also possible to add constructors and methods to `struct`s.
MKroehnert
A: 

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.
Daemin
+3  A: 

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 };
Roger Nelson