joli 0 Posted July 29, 2016 Report Share Posted July 29, 2016 (edited) This is just an example for who needs something alike also to c++ experts comments that are very welcome.Purpose: .create a list of objects and do something with them Device: pic18Fxxx // file: students.h #ifndef _STUDENTS_H_ #define _STUDENTS_H_ typedef unsigned char uchar; #define STUDENTS 64 // max number of students class Student // { private: uchar s_id; // data member uchar s_age; // data member public: void AddStudent( uchar *pustudent ); // function member }; extern Student StuList[ STUDENTS ]; // studets list #endif // file: student.cpp #include <system.h> #include "students.h" class Student StuList[STUDENTS]; // list of students void Student::AddStudent( uchar *pstudent ) // AddStudent { s_id = *pstudent++; // update id s_age = *pstudent; // update age } void main() { uchar adata[2]; // buffer from where data comes from for( uchar i=0; i<STUDENTS; i++ ) // Lets fill all students [0..63] { adata[0] = i+1; // data to be stored adata[1] = i+1; StuList[i].AddStudent( adata ); // add student id&age } while(1); } i did AddStudent member this way because i needed to pass a buffer with data received from the serial port. It works perfect, but suggestions are welcome. I also did it with an array of structures that results shorter in ram resources. Using classes an additional byte is consumed for each element. br Edited July 31, 2016 by joli Quote Link to post Share on other sites
joli 0 Posted July 31, 2016 Author Report Share Posted July 31, 2016 Forgot to mention the basis of it is on boostc++ manual page 83. br Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.