If I have a bunch of structures defined within a class header file but outside the scope of the class how would I set a SETTER function to change the values.

struct VoltageValues
{
        float 		Vcc;
	int		TblkLoopEnb;
        double     Blah;
};

class TblkArray
{
      public:
      TblkArray();
      ~TblkArray();

      struct VoltageValues *getVoltageValues();
      void setVoltageValues;???????????????????

      private:
      struct VoltageValues myVoltageValues;
      int IgnoreFail;
      unsigned char BitIgnore;
};

I'm not sure how to setup the SETTER function(setVoltageValues) to access the elements of the structure. Thanks.

Recommended Answers

All 3 Replies

Structure doesn't have restricted acces. You have "myVoltageValues", so simply do:
>myVoltageValues.Vcc = 3.14;
>myVoltageValues.TblkLoopEnb = 3;
>myVoltageValues.Blah = 2.3333333;

>>Structure doesn't have restricted acces
In c++ they do -- structures are nearly identical to c++ classes, except the default access type is public. TMK there are no other differences between a structure and a class.

A few suggestions:

void setVoltageValues(float fVcc, int iTblkLoopEnb, double dBlah);

Or:

void setVoltageVcc(float fVcc);
void setVoltageTblkLoopEnb(float iTblkLoopEnb);
void setVoltageBlah(double dBlah);

However, if you change your struct, the methods above have to change.

Or:

void setVoltageValues(struct VoltageValues *pVV);

Pass in a pointer to a pre-initialized VoltageValues struct and assign the internal members to the corresponding ones in the class variable.

Whichever you feel will be easier to maintain and understand; any of the ways will work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.