Luis Moreira wrote: > I apologise for not posting anything yet about this subject, but I am > trying to process all the information about it. No problem at all. > The problem I have at the moment is that I just discovered that the CSS > C compiler does not deal very well with function pointers especially in > structs, hence is delaying my research into the subject. > > The first issue I had to overcome was the fact that I am not only > writing a program using OOP I am also writing the infrastructure to > generate the objects and actually that is the most important part. I > will also start by not having inheritance but I can see how that could > be done. The other issue I had is the "VTable" I could not understand > its function. As I've mentioned several times before, we are not using virtual functions. I think the benefit you get, does not warrant the extra overhead and complexity. Let me make this clear: we DO NOT have the infrastructure to create objects. We create "data-only" objects dynamically when it is warranted, but they don't have pointers to functions, they're just data (structures). Functions are the normal, static C functions that manipulate the data. > So this is how I am doing it: > > I have a function called checksumCalculator() which takes two parameter, > an int bufferSize and an array of chars buffer which is the packet. It > returns a char checksumCal which represents the checksum. A noun is a bad name for a function. A better name would be something like: PacketHandler_CalculateChecksum(int bufferSize, char *buffer) > To transform it into a class I created three, setSize, setBuffer, > calculatechecksum. I also have three fields, bufferSize, buffer and > checksumCal. > > So far all seems ok, but then what I envisage is a struct with the three > fields and three function pointers one for each of the methods. When an > instance of the class is created the three fields are initialised and > the function address passed to the pointers. Is this what you do? Not at all. We do not instantiate objects. I described what we do in other posts, it is fairly simple, which leads me to believe that you are overthinking it. In your case, the object would be: - A file called PacketHandler.c, which contains public and private functions - A file that exports public functions and data, PacketHandler.h - A structure of type TPacket, that has the members size, buffer, and checksum: typedef struct { int size; char *buffer; char checksum; } TPacket; You can then either declare your rxPacket and txPacket statically, or create them dynamically (using calloc()) at runtime. > I found that when I try this I can not manipulate the fields of the > struct with the class methods, how did you get around that? We sidestep the issue by avoiding having virtual functions. :-) > I am probably making a mess out of this, so apologies in advance. No need to apologize. The setup that we have now, is the result of many months of trial and error. We followed many many blind alleys until the whole picture finally emerged. And we're still making improvements as we go. I just hope I can help save you some grief. Vitaliy -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist