Need help with class function

I am creating a simple c++ class to manage a series of menu items. Each menu item has: an LED pin a display buffer a state

I'd like to init the class and save a different display buffer (a single INT) for each menu. An object will be instantiated for each menu item.

There should be set, get and put functions

set will store the address of the INT buffer get will return the value stored in the INT buffer put will place a new value in the INT buffer

Here follows simplified code of how I think the set function would be done:

class menuMgr() { /* this should save the address of the buffer in the class object /* void set_displaybuff(&buffer) { _buf = buffer; }

I'm need help with the c++ syntax for the get and set functions, where _buf is the private var that holds the data.

=Alan R. :fearful:

I'm not sure why you want a different object for each menu item. Couldn't you just have an array of objects and initialize each one differently?

Usually, a set method assigns a new value into a particular member variable of the class. All error checking on the new value usually resides in these methods. The set method is what you refer to as a put method. What you call a set should probably just be a different set method that has a different parameter which gets assigned into a pointer. Therefore, you will have two different set methods: 1) a pointer to hold the INT buffer address, and 2) an int to hold the new INT value.

A get method simply returns that value. A skeleton might be something like:

class menuMgr{
   public:
      void setBufferLvalue(int *ptr);
      void setMenuInt(int val);
      int getIntBuffer();
   private:
      int *bufferLvalue;
      int menuInt;
      int ledPin;
}

It would seem to me, then, that you could just create an array of these objects to do the task at hand.

I'd use pointers:

class menuMgr {
  private:
    int *_buf;
  public:
    void set(int *b) {
      _buf = b;
    }

    void put(int v) {
      *_buf = v;
    }

    int get() {
      return *_buf;
    }
};

...
int myBuffer;
menuMgr instance;
...
instance.set(&myBuffer);
instance.put(34);
int val = instance.get();

Guess I didn't word my question very well. I do know how to set up a Class - just didn't 'get' the syntax to work with pointers. I prob didn't even need to say it was going to be in a Class... Yes - an array would do fine - this is a learning-thing for working with classes.

In my thinking. if i SET the display buffer attribute, I'm assigning it's address or location. then if i GET from that address, I retrieve the contents, and if I PUT to that buffer I store (PUT) a new value in to it. All semantics I guess - but then that is what OO is all about!

econjack: I'm not sure why you want a different object for each menu item. Couldn't you just have an array of objects and initialize each one differently?

Usually, a set method assigns a new value into a particular member variable of the class. All error checking on the new value usually resides in these methods. The set method is what you refer to as a put method. What you call a set should probably just be a different set method that has a different parameter which gets assigned into a pointer. Therefore, you will have two different set methods: 1) a pointer to hold the INT buffer address, and 2) an int to hold the new INT value.

A get method simply returns that value. A skeleton might be something like:

class menuMgr{
   public:
      void setBufferLvalue(int *ptr);
      void setMenuInt(int val);
      int getIntBuffer();
   private:
      int *bufferLvalue;
      int menuInt;
      int ledPin;
}

It would seem to me, then, that you could just create an array of these objects to do the task at hand.

Thanks majenko - that is what I was looking for! Prob didn't need to say it was in a class... XD

majenko: ``` I'd use pointers:

class menuMgr {  private:    int *_buf;  public:    void set(int *b) {      _buf = b;    }

   void put(int v) {      *_buf = v;    }

   int get() {      return *_buf;    } };

... int myBuffer; menuMgr instance; ... instance.set(&myBuffer); instance.put(34); int val = instance.get();

Why are you using pointers at all? For ints, pointers and values are the same size. Manipulating values is easier than manipulating pointers. I can't see any reason, in that class, to be using pointers at all.

As that code is written, the instance owner owns the memory. It doesn't need to be asking the class to save a value in the memory. Nor does it need to be asking the class what is in the memory location. Nor does the class do anything with the data in the memory location.

Paul - This is an egg-laying-milk-pig, VERY glitzy interface to a stepper motor to cut gears and step work-pieces on a milling machine. I'm doing 'fancy' just because i can.

Why am I using a class for this structure? Well - just to work with classes for one thing.It also allows a for a simple dynamic indexing structure set up such that turning a rotary encoder increments the LED's for each menu option. So the code gets simplified (in some respects) to being able to always make this reference:

MenuList[Curmenu].do_somefunc() such as set, get put whatever for each LED and current menu item. Also when the SWITCH is on the rotary encoder is depressed, I can perform whatever activity is assigned to that menu by the same syntax.

I have a number of MenuItem objects collected an array of MenuItems[]. During init, each menu item is set up with it's various values and then added to the array. All the code needs to do is monitor the output of the encoder. In IDLE the rotary encoder increments modulo MENU_ITEM_CT. When switching to EDIT, (by depressing the rotary encoder knob) the rotary encoder goes to MenuList[CurMenu].put_display(). When the rotary encoder is back in IDLE, (second depress of RE knob), as it is rotated thru the menu options, I can send the current value for that menu to the display via MenuList[Curmenu].get_display(), and later I can even send that value to operate a stepper motor - where appropriate.

Hope that meets with your approval - it works for me! XD

=Alan R.