Jump to content


Photo

Array Of Objects Boostc++


1 reply to this topic

#1 joli

joli

    Regular

  • EstablishedMember
  • Pip
  • 81 posts
  • Gender:Male
  • Location:Portugal

Posted 29 July 2016 - 08:10 PM

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 by joli, 31 July 2016 - 04:55 PM.


#2 joli

joli

    Regular

  • EstablishedMember
  • Pip
  • 81 posts
  • Gender:Male
  • Location:Portugal

Posted 31 July 2016 - 05:01 PM

Forgot to mention the basis of it is on boostc++ manual page 83.

 

br





Reply to this topic



  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users