0

In C++, I'm trying to decide whether or not to use a struct or class. So far I understand that both are more or less identical, but a class is used more often when private variables are needed or to group the concept of an object that performs actions.

But what I'm wondering is: Does including functions within a class increase the memory requirements (as opposed to having structs, with functions separated)?

Here is an example

class Vector2D {
    public:
        int x;
        int y;
        Vector2D();
        getMagnitude();
        Normalize();
}

vs.

struct Vector2D {
    int x;
    int y;
}

// Some functions defined separately
getMagnitude(Vector2D v);
Normalize(Vector2D v);

And if I have a bunch of other vector functions, say, adding two of them together and I have a vector class, is it better if those are included in the class, so you have a function like addToVector(Vector2D v2) or to keep these multi vector functions outside of the class so that you'd have an addToVector(Vector2D v, Vector2D v2)?

I understand the second question might lean towards opinion, but if there is a "best practice" defined way, I'd like to know.

Thank you

Sean Payne
  • 1,625
  • 1
  • 8
  • 20
  • 1
    This seems to be less about effeciency and more about style. For style look [here](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c2-use-class-if-the-class-has-an-invariant-use-struct-if-the-data-members-can-vary-independently). For efficiency, note that there is no difference between these methods. – Fantastic Mr Fox Jun 24 '21 at 02:07
  • 1
    There is no difference of efficiency between a class and a struct. They are the same thing, except that access to all members (including member functions) and bases is `private` by default for classes, and `public` by default for structs. If you are passing your data structure to C code, it is generally preferable to use the *same definition of that type* in both C and C++, so it is often preferable to use a `struct`. Beyond that, the choice is stylistic. – Peter Jun 24 '21 at 02:16

1 Answers1

5

C++ Efficiency of Classes vs Structs

Structs are classes. There is zero efficiency between one and the other.

Does including functions within a class increase the memory requirements (as opposed to having structs, with functions separated)?

No. There is no practical difference between "memory requirements" of member functions and non-member functions.

Also, structs can have member functions. All classes can have member functions, and structs are classes.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Worth noting that *virtual* member functions do impose some additional overhead, in terms of a vtable and possible dynamic lookup in some situations. But regular member functions are, as far as the compiler is concerned, ordinary functions with a `this` argument – Silvio Mayolo Jun 24 '21 at 02:14
  • That's worth noting, but also not actually a _difference_ between classes and structs so much as a difference between classes with and without virtual methods. – Nathan Pierson Jun 24 '21 at 02:25
  • If you actually look at the ancient history in which C++ started life being described as an extended C ("C with classes", etc), and early teaching material described C++ structs and classes as being "extended" forms of C structs, it's probably more accurate to say that classes are structs rather than saying that structs are classes. (Yes, I'm being a little tongue-in-cheek, but that is how C++ was described and taught in its early days) – Peter Jun 24 '21 at 02:39
  • @Peter `early teaching material described C++ structs and classes as being "extended" forms of C structs` I'd say that's still fairly accurate at least as a description. But of course, C++ structs are also "extended" forms of C structs. – eerorika Jun 24 '21 at 04:02