How to choose between structure and class?

 

In this topic, I propose to either create some list of rules for choosing between a structure and a class, or state that both options are equivalent in terms of performance.

 

I googled the differences between a class and a struct, but they vary depending on the programming language.

Here are the differences that are given in the documentation (link):

Classes differ from structures in the following:

  • the keyword class is used in declaration;
  • by default, all class members have access specifier private, unless otherwise indicated. Data-members of the structure have the default type of access as public, unless otherwise indicated;
  • class objects always have a table of virtual functions, even if there are no virtual functions declared in the class. Structures cannot have virtual functions;
  • the new operator can be applied to class objects; this operator cannot be applied to structures;
  • classes can be inherited only from classes, structures can be inherited only from structures.

Classes and structures can have an explicit constructor and destructor. If your constructor is explicitly defined, the initialization of a structure or class variable using the initializing sequence is impossible.

But what if these differences don't matter?

 
Vladislav Boyko #:
But what if these differences don't matter?

Let's look at an example close to the real one.

Here's some kind of structure:

enum ENUM_MO_STATUS
  {
   MO_STATUS_NOT_EXISTS = 10,
   MO_STATUS_EXISTS     = 11,
   MO_STATUS_CLOSED     = 12
  };

struct STRUCT_MORDER
  {
   double         stopLoss;
   double         takeProfit;
   double         price;
   double         volume;
   double         commission;
   double         swap;
   double         profit; 
   datetime       opTime;
   datetime       expir;
   int            type;
   int            ticket;
   ENUM_MO_STATUS status;
                  STRUCT_MORDER() { status = MO_STATUS_NOT_EXISTS; }
  };

Next, I will take 2 dynamic arrays of such structures, add several simple variables and even a string:

STRUCT_MORDER buy[];
STRUCT_MORDER sell[];
double        someVar1;
long          someVar2;
bool          someVar3;
string        someText;

Now I want to wrap this into a single object. Maybe I'll even add a couple of primitive methods there. But still, it doesn't make any difference to me whether it's wrapped in a class or a struct. It will be equally convenient for me to use both.

How do I make a choice? Or in this case there will be no difference (including in terms of performance)?

 
Vladislav Boyko #:
I googled the differences between a class and a struct, but they vary depending on the programming language.

  • C++
    I don't fully understand, but it looks like structure and class are the same thing
  • Visual Basic
    Structures are value types; classes are reference types. A variable of a structure type contains the structure's data, rather than containing a reference to the data as a class type does. Structures use stack allocation; classes use heap allocation.
  • MQL
    ???
 
Vladislav Boyko #:

I googled the differences between a class and a struct, but they vary depending on the programming language.

Here are the differences that are given in the documentation (link):

But what if these differences don't matter?

There is only 2 points that really matters objectively :

1. Do you need to use pointers ?

2. Do you need virtual methods ?

Otherwise said, do you need dynamic polymorphism ? If you answer yes to one of the question then you need a class. Otherwise you can use class or structure following your preference.

I would add that if you are unsure, prefer a class, this way you can easily use pointers or virtual methods later, even if you think you don't need it now.

 
Alain Verleyen #:

There is only 2 points that really matters objectively :

1. Do you need to use pointers ?

2. Do you need virtual methods ?

Otherwise said, do you need dynamic polymorphism ? If you answer yes to one of the question then you need a class. Otherwise you can use class or structure following your preference.

I would add that if you are unsure, prefer a class, this way you can easily use pointers or virtual methods later, even if you think you don't need it now.

Thank you very much!

 

Structures are much better than classes in some cases.


POD-structures:

 
fxsaber #:
File-operations.

Yes, I had a thought that even though a struct is a subset of a class, a struct cannot always be replaced by a class. For example, if something external expects a structure (passing to a DLL function).

fxsaber #:
union.

The Microsoft C++ documentation says that a union is also a class type along with a structure:

The three class types are structure, class, and union.

But the use cases for union seem to be more obvious (although I haven't had to use union yet and I don't program in C++😄).


Actually, it's logical to use a structure if you need to pass it to a DLL, and it's logical to use a class if you need a pointer or virtual methods. In such cases, you simply have no choice, so the question of choice does not arise😄

The question arises in the case when you do not need distinctive features and you will be equally comfortable using both a class and a structure. I think Alain's post is a comprehensive answer to this question.

 
Vladislav Boyko #:

Yes, I had a thought that even though a struct is a subset of a class, a struct cannot always be replaced by a class. For example, if something external expects a structure (passing to a DLL function).

The Microsoft C++ documentation says that a union is also a class type along with a structure:

But the use cases for union seem to be more obvious (although I haven't had to use union yet and I don't program in C++😄).


Actually, it's logical to use a structure if you need to pass it to a DLL, and it's logical to use a class if you need a pointer or virtual methods. In such cases, you simply have no choice, so the question of choice does not arise😄

The question arises in the case when you do not need distinctive features and you will be equally comfortable using both a class and a structure. I think Alain's post is a comprehensive answer to this question.

I find embedding structures into classes is a good option - you can decide when to pass data as an object, or when to pass it as the sub structure (e.g. the dll requirement you mention), without losing the benefits of OOP.

This also helps when I need to copy a set of fields - if they are grouped together in a structure it is just 1 line of code
 
Vladislav Boyko #:

The Microsoft C++ documentation says that a union is also a class type along with a structure

This is not the case in the MQL5.

 

I like to use structs for the unique top level "holder" of things , but i like pointers in classes .

Plus they make my code look like javascript and remind be of my teens.

Reason: