Use struct as base for derived class in C++

c, c++, casting

Solution

You just derive from the C struct. In C++, the only difference between a `struct` and a `class` is that the latter's default member accessibility is `private`, while the former's is `public`.

If only single inheritance is involved, the `class`'s address should be the same as the `struct`'s which acts as a base class. If the inheritance only serves the implementation (that is, there is no Is-A relationship between the two), consider using private inheritance.

Problem

Is it possible to have a class inheriting from a struct? More specifically, how can I wrap a C struct with a class, so that I can pass class pointers to methods that requires struct ptr and cast back when I receive the pointer in e.g. callbacks? (Or even more specifically, the address of the class should be same same address as the struct...)

Original source