Structures

C struct vs C++ class

C struct

  • functions are loosely correlated with paramaters,
  • parameters are usually passed via pointer,
  • it may look like array for struct with 2 identical parameters in struct,
  • for structures created dynamically look for malloc with non-usual size.

C++ class

  • Find constructor, it have always 1 argument (this pointer -> thiscall),
  • main() function have initialization function __main with ctor initlizers,
  • after creation of class with new operator the class constructor is called (can be empty),
  • Methods are called with thiscall convention.

Inheritance

Constructors of base class are called 1st in child class, additionally assigment to variables in class definition is put inside constructor:

void __thiscall Inherited(Inherited *this)
{
  Box((Box *)this);
  this->c = 1; // this was assigned in class definition, not constructor
  this->b = 3;
  this->a = this->b;
  return;
}

Inherited destructors contains destructors of base class:

void __thiscall ~Inherited(Inherited *this)

{
  ~Box((Box *)this);
  return;
}

Virtuals

Generally we have the same behaviour like in inheritance, but additionally we have also vtables.

The structures of classess contains 1 additional field (at offset 0): vptr to vtable structure containing pointers to all virtual methods in this class:

struct vtable_class_A {
  void* virtual_func_1();
  void* virtual_func_2();
  // ...
};

References

  1. OA Labs