Using pointer to structure in a class

Hi.

I am new at writing classes. After a couple successes, I am fighting the syntax to work in a structure.

// In .ino
#define KEY_M_START_L 32

typedef struct st_contact {byte b_status; byte c_trigger; byte b_enable; 
                           byte b_running; unsigned long ul_TS; 
                           long l_TS_timeout; unsigned int ui_address;};
st_contact start_L = {99, -1, true, false, 0, 0, KEY_M_START_L};
// In .H

class cl_contact{
private:
  void init(void);

public:
  cl_contact (struct st_contact *edit_contact); // overdeclaring?
};
// In .cpp

cl_contact::cl_contact(struct st_contact *edit_contact){           
      struct st_contact *contact=edit_contact;
      // THIS IS WHERE I AM IN TROUBLE
 //   this->edit_contact=edit_contact;
 //   byte i;
//    i=contact->b_status;
 //   Serial.println(contact->b_status);
    
    init();      
};

void cl_contact::init(void){
 
};
// again in .ino setup()

cl_contact sw_start_L(&start_L);

In the last block of code I try to declare a pointer to a st_contact structure called contact and make it point to edit_contact which contains (I suppose) a pointer to start_L. I am not sure i need to declare a local pointer contact pointing to the same address as edit_contact which seems the same as the initial start_L

How can I get and set the structure members, like the variable b_status?

I hope I did not mess the explanation :slight_smile:

Thanks
H. Martins

You cannot point to a structure definition. You can instantiate one of the data structures as a class member and then base your methods off that instance.

It may help to think of a C++ class as a structure with all the code to manage the data for the structure built-in to the 'structure' (ie the class).

As the class 'hides' this data structure from you, to find, add, delete, change data, etc, you need to write methods that access the class' data for you. Hence the need for set/get methods all over the place. This is usually a lot more work but up front, done well, will provide data encapsulation and reusable code, thus saving you time the next time around.

Depending on the application, sometimes it is easier to just use C constructs.

marco_c:
It may help to think of a C++ class as a structure with all the code to manage the data for the structure built-in to the 'structure' (ie the class).

For purposes of this discussion, in c++, a struct and a class are EXACTLY the same thing, except that member data of a struct is, by default, public, while member data of a class is, by default, private. Either of those can be over-ridden in the definition, so struct and class can be used inter-changeably.

      // THIS IS WHERE I AM IN TROUBLE
 //   this->edit_contact=edit_contact;

You are in trouble because your class doesn't have a member variable named "edit_contact". If you want one, put this line in your class declaration:

    struct st_contact *edit_contact;

class st_contact {

public:
st_contact(this n that);
virtual ~st_contact(void);

..All your methods and things. Some could be virtual..
including..

byte b_status;
byte c_trigger;
byte b_enable;
byte b_running; unsigned long ul_TS;
long l_TS_timeout;
unsigned int ui_address;
};

Now what you would like to do with your cl_contact and the info you need to do it is in one tidy package. No need for odd nonsense. You ARE the struct. This is what @RayLivingston was trying to tell you. I least that's what I got from it.

-jim lee

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.