View previous topic :: View next topic |
Author |
Message |
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
how use struct similar to class? |
Posted: Tue Apr 28, 2020 9:38 am |
|
|
I don't know many about c, but i can undertand so how i can make a struct similar like a class of this way?
I imagine this
Code: | struct subscriptions{
char topic;
int len;
int qos;
char data;
}; |
for will make 5 "subscriptions" and keep data in everyone member. and after consut info.
Code: | for (int i=0; i<5; i++){
subscriptions[i]->topic ="hi";
} |
|
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Tue Apr 28, 2020 9:48 am |
|
|
I've done this before by creating a struct, and then for the constructor I'd call it an "init" function.
For all the member functions, I would pass a _this pointer as a parameter, and then I could make it behave somewhat like a class.
Code: | typedef struct
{
uint8_t var1;
uint8_t var2;
} myclass_t;
void myclass_t_init(myclass_t *_this, uint8_t parameter1, uint8_t parameter2)
{
_this->var1 = parameter;
}
void myclass_t_set_var2(myclass_t *_this, uint8_t parameter)
{
_this->var2 = parameter;
}
uint8_t myclass_t_set_var2(myclass_t *_this)
{
return _this->var2;
} |
|
|
|
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
|
Posted: Tue Apr 28, 2020 10:02 am |
|
|
dluu13 wrote: | I've done this before by creating a struct, and then for the constructor I'd call it an "init" function.
....
return _this->var2;
} |
Thank you, but i want to know if "struct" can be like variable[index] with many members maybe as a database.
Maybe this is not possble in c? |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Tue Apr 28, 2020 10:19 am |
|
|
cvargcal wrote: | dluu13 wrote: | I've done this before by creating a struct, and then for the constructor I'd call it an "init" function.
....
return _this->var2;
} |
Thank you, but i want to know if "struct" can be like variable[index] with many members maybe as a database.
Maybe this is not possble in c? |
It is. I do it myself. However, your line here:
Code: | subscriptions[i]->topic ="hi"; |
has a problem. You are using the arrow operator which is like calling a member variable and dereference. However, calling an index (the square brackets) of your subscription variable is already a dereference of the pointer.
You want to do this:
Code: | subscriptions[i].topic = ...;
|
Also, char means it is only one character like 'c'. You will need to make a char array of known size if you want to store a string. Or, you will need to make it a char pointer to an array that you dynamically allocate later at run time... |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1354
|
|
Posted: Tue Apr 28, 2020 11:51 am |
|
|
The only real tricky part to emulating classes in C is if you want to implement dynamic dispatch (virtual function overrides in C++, any function overrides in Java, etc.). Inheritance can be emulated via composition as long as you are using static dispatch, but once you decide to go dynamic, it can get much trickier. And not only is it tricky, it involves function pointers, which are notoriously slow operations on a PIC.
There are various patterns to handling it if that is your end goal. I have one for a 'detached" style interface object method. I'm sure others have their own patterns as well. |
|
|
|