Basically structure could be extended:
Code: Select all
struct coords2d {
int x;
int y;
}
struct coords3d extends coords2d {
int z;
}
Pointers to coords2d and coords3d would be "related" so casting would be possible between them. This solves a common problem in C when you need to have structures that share a common part and you need to do little hacks, see thread lists in ChibiOS for example.
Code: Select all
struct element {
element *next;
element *prev;
}
struct ready_list extends element {
...
}
struct thread extends element {
...
}
Next step is to associate functions to structures (but also other types), this would mean methods, note the "this" keyword:
Code: Select all
void element:dequeue(void) {
this->next->prev = this->prev;
this->prev->next = this->next;
}
element *el;
el->dequeue();
void uint32_t:increase(void) {
this = this + 1;
}
void uint32_t:clear(void) {
this = 0;
}
uint32_t n;
n.clear();
n.increase();
We could also thing to static methods, methods that can be called on the type not an actual type instance.
Code: Select all
void element::reset(void) {
...
}
element.reset();
I think there is no need to have a specific class constructs, any type can do simple things like this. Virtual methods could also be considered. Complex things like runtime types checking, I think, would be out of scope.
Types as namespaces for constants
This is a bit Java-like, one of the C problems is that enums have no namespace so conflicts are common. It would be possible to declare constants with type associated to structures or other types this way.
Code: Select all
/* declaration */
const uint8_t element::LINK = 0;
const uint8_t element::UNLINK= 1;
const uint8_t uint8_t::ZERO = 0;
const void * ptr_t::NULL = (void *)0;
const element * element::NULL = (element *)0;
/* use */
uint8_t operation = element:LINK;
uint8_t i = uint8_t:ZERO;
Those constant would:
- Not allocate memory, pretty much like #define as use.
- Have a specified type unlike enums.
- Have a namespace (the type name).
Giovanni