Pure Virtuousness

Last time I talked about using C++ to develop a simple class library for creating LCD display-style user interfaces (you can find the evolving code here).

To refresh your memory, the main class is (more or less) here:

class lcdui
  MENU *menu;   // the current menu
  unsigned int current;  // current index in the menu
  // constructor
   lcdui(MENU *_menu) : menu(_menu) { current=0; };
  // execute the menu
  virtual void go(unsigned int menulevel=0);
  virtual void callback(int id, MENUTYPE mtype, EVTYPE event, int *value=NULL);
  // override to handle T_ACTION menu items
  virtual void dispatch(int id) { };
  // called when no input available
  virtual void idle(void) {};
  // get an input code
  virtual INTYPE  getInput(void) =0;
  // Write to the LCD
  virtual void output(std::string ostring) =0;

I mentioned last time that C++ doesn’t perform any magic that the C compiler can’t do. It does some things automatically, of course. More important in some cases, it doesn’t do things.

For example, consider this simple class:

class ademo
    int a;
    ademo(int _a=0) { a=_a; }  // could use this.a=a;
    int geta(void) { return a; }

Nothing hard here. In C, the same construct might look like this:

typedef struct
    int a;
    } ademo;

void construct_ademo_default(ademo *this)

void construct_ademo(ademo *this, int _a)

int geta(ademo *this)
  return this-a;

This isn’t ideal, of course. You don’t really have any direct tie between the structure, the constructors, or the member function other than in your own mind. You can easily create a structure and call geta() without first constructing it, for example. There are no default arguments in the traditional C compiler, so you have to remember that too.

Leave a comment

Your email address will not be published. Required fields are marked *