Daniel Sloof
Daniel Sloof

Reputation: 12706

Class vs Struct for data only?

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:

Upvotes: 14

Views: 17359

Answers (7)

Roger Nelson
Roger Nelson

Reputation: 1912

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

Upvotes: 8

CB Bailey
CB Bailey

Reputation: 791421

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.

Upvotes: 6

Dominik Grabiec
Dominik Grabiec

Reputation: 10655

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.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

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.

Upvotes: 0

S.Lott
S.Lott

Reputation: 391818

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

Upvotes: -1

Evan Teran
Evan Teran

Reputation: 90422

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.

Upvotes: 24

Jason Baker
Jason Baker

Reputation: 198527

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.

Upvotes: 3

Related Questions