For example:

struct st { int n = 0; int m = 0; int summa (int i, int j) { return n + m; } } 

The code, of course, does not work, but is it possible to do something similar in C?

  • 6
    For this, Uncle Bjarne came up with C ++ - wololo

2 answers 2

In this form - no, this is not C ++. But nothing prevents you from transmitting this explicitly.

By the way, there is no namespace here either, so the function will have to be pulled out of the structure.

In general, C is a low-level language, without embellishment and syntactic sugar. So if you please lay out the entire program on the shelves.

 struct st { int n; int m; }; void st_constructor(struct st* this) { this->n = 0; this->m = 0; } int st_summa(const struct st* this) { return this->n + this->m; } 
  • Yes, I do so - it just became interesting - Andrej Levkovitch

No, in C, structures cannot have member functions.

Of course, there may be function pointers, but this is not at all, because it takes place in each instance of the structure and does not receive an implicit this when called.