Use struct as a base for a derived class in C ++

Is it possible to inherit a class from a structure?

More specifically, how can I wrap a C structure with a class so that I can pass class pointers to methods that require struct ptr and are discarded when I get a pointer, for example. callbacks?

(Or, more specifically, the class address must be the same address as the structure ...)

+3
source share
3 answers

You simply extract from C. In C ++, the only difference between structand a classis that the last element availability is equal by default privateand the first is public.

, class , struct, . (.. Is-A), .

+4

, ++ - , - . .

A B OO-: B A: B A, : make A - B.

+2

, struct - , ;

struct person
{
};

:

class person
{
  public: //...
};

, ( ) private, .

struct person {...};
struct employee : person {...}; 

, ISA ;

class person {...};
class employee : person {...};

Means private inheritance (i.e., an employee EXECUTED IN THE CONDITIONS of a relationship and not implying a conversion between the employee and the person).

In your case, classes and structures are interchangeable;

+2
source

Source: https://habr.com/ru/post/1730750/


All Articles