Jump to content

Array Of Objects Boostc++

Recommended Posts

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                                   // 
        uchar s_id;                             // data member
        uchar s_age;                            // data member
        void AddStudent( uchar *pustudent );    // function member

extern Student StuList[ STUDENTS ];             // studets list

// 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

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.



Edited by joli
Link to post
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...