CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

how use struct similar to class?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

how use struct similar to class?
PostPosted: Tue Apr 28, 2020 9:38 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Apr 28, 2020 9:48 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 10:02 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Apr 28, 2020 10:19 am     Reply with quote

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: 1317

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 11:51 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group