how to create C-type that inherits another C-type
how to create C-type that inherits another C-type
I know how to create types using C and how to use inheritance in .py but how do you make one type defined in C inherit another type defined in C?
Re: how to create C-type that inherits another C-type
Well you don't really inherit in C. But the normal way is to include the base structure as the first element of the derived structure.
Something like this:You'd then access the fields using foo.base.a, foo.base.b, and foo.c. &foo and &foo.base are the same, so you can pass pointers to foo around as if they were base_t * or derived_t *.
If you don't want the additional dereference, then you need to use macros to create the elements of the base structure. Something like this:
Something like this:
Code: Select all
typedef struct {
int a;
int b;
} base_t;
typedef struct {
base_t base;
int c;
} derived_t;
derived_t foo;
If you don't want the additional dereference, then you need to use macros to create the elements of the base structure. Something like this:
Code: Select all
#define BASE_FIELDS \
int a; \
int b;
typedef struct {
BASE_FIELDS
} base_t;
typedef struct {
BASE_FIELDS
int c;
} derived_t;
derived_t foo;
how to create C-type that inherits another C-type
But with the current runtime how would you access the members in .locals_dict of the base with a reference to derived?dhylands wrote:Well you don't really inherit in C. But the normal way is to include the base structure as the first element of the derived structure.
Something like this:You'd then access the fields using foo.base.a, foo.base.b, and foo.c. &foo and &foo.base are the same, so you can pass pointers to foo around as if they were base_t * or derived_t *.Code: Select all
typedef struct { int a; int b; } base_t; typedef struct { base_t base; int c; } derived_t; derived_t foo;
If you don't want the additional dereference, then you need to use macros to create the elements of the base structure. Something like this:Code: Select all
#define BASE_FIELDS \ int a; \ int b; typedef struct { BASE_FIELDS } base_t; typedef struct { BASE_FIELDS int c; } derived_t; derived_t foo;
If base has member b1 and you do
Code: Select all
d = derived()
d.b1()
Also MP_OBJ_IS_TYPE(obj, base) doesn’t currently work if obj is a derived.
Re: how to create C-type that inherits another C-type
OK- I was talking about how you might go about it in C in general and not specifically in MicroPython.
MicroPython doesn't currently have any mechanisms to use any type of C inheritance, so changes would need to be made to support whatever mechanism you decided to use.
MicroPython doesn't currently have any mechanisms to use any type of C inheritance, so changes would need to be made to support whatever mechanism you decided to use.
Re: how to create C-type that inherits another C-type
Will this be implemented?dhylands wrote:OK- I was talking about how you might go about it in C in general and not specifically in MicroPython.
MicroPython doesn't currently have any mechanisms to use any type of C inheritance, so changes would need to be made to support whatever mechanism you decided to use.