Have homework and we just went over classes. Thinking about how I could implement classes into my homework but the instructions say use a structure. (It's reading binary files and editing information) After looking at some stuff we did in class I probably don't need classes for my situation. But now I'm curious, why would you want to use a structure inside a class? Why would you use a structure when you can just set the data members in the class instead? What does struct have to offer to warrant doing this?
-
1There is no difference between a structure and a class. – Marc Glisse Mar 01 '19 at 02:43
-
[When should you use a class vs a struct in C++?](https://stackoverflow.com/q/54585/2682312) – Axalo Mar 01 '19 at 02:46
-
1Are you asking why you should use a `struct` *inside of* a `class` (i.e. a `struct` declaration nested inside of a `class` declaration) or *instead of* a `class` ? – Jeremy Friesner Mar 01 '19 at 02:52
-
structs are useful for bundling information together to reduce the amount of arguments to pass to functions. – R.S. Mar 01 '19 at 03:24
2 Answers
The primary benefit of putting values into an inner struct
as opposed to just declaring them as member variables of the class would be that you can then instantiate multiple instances of that struct very easily, and refer to the set of variables in each struct with a single pointer or reference.
As an example, here are two implementations of a toy array-of-3D-points class. The first one just uses separate member-variables, while the second one declares an inner struct
to represent a Point
object.
Note that in the first implementation, the RotatePoint()
method takes four arguments, while in the seconds argument it takes just two. Being able to refer to a Point with a single struct Point &
argument (rather than three separate float &
arguments) is both more efficient at run-time and less error-prone for the programmer to call.
// implementation without an inner struct
class MyPointArray1
{
public:
[...]
void RotatePoints(float radians)
{
for (int i=0; i<POINTS_ARRAY_LENGTH; i++)
{
// Hazard here -- a tired programmer might specify arguments in the wrong order!
RotatePoint(x[i], y[i], z[i], radians);
}
}
private:
enum {POINTS_ARRAY_LENGTH = 100};
float x[POINTS_ARRAY_LENGTH];
float y[POINTS_ARRAY_LENGTH];
float z[POINTS_ARRAY_LENGTH];
void RotatePoint(float & x, float & y, float & z, float radians)
{
// [math to update the values of x, y, and z would go here]
}
};
// implementation with an inner struct
class MyPointArray2
{
public:
[...]
void RotatePoints(float radians)
{
for (int i=0; i<POINTS_ARRAY_LENGTH; i++)
{
// It's pretty much impossible to get this wrong without provoking a compile-time error :)
RotatePoint(points[i], radians);
}
}
private:
enum {POINTS_ARRAY_LENGTH = 100};
struct Point
{
float x;
float y;
float z;
};
struct Point points[POINTS_ARRAY_LENGTH];
void RotatePoint(struct Point & pt, float radians)
{
// [math to update the values in (pt) would go here]
}
};

- 70,199
- 15
- 131
- 234
-
Note than an inner `class` would serve as well as an inner `struct` -- the only difference between the two keywords is that in a `class`, the default protection-level for member-variables is `private` while in a `struct` the default protection-level is `public`. – Jeremy Friesner Mar 01 '19 at 03:09
From my understanding, you would implement a struct inside a class when you want to create objects within that class only. Outside of the class, you would not be able to create objects of that struct.

- 61
- 3