I am trying to update my old C code to C++ , and trying to grasp C++ at the same time.
It's not clear to me when creating a class, which variables I should declare as private (which are basically behaving as global variables to member functions) verses those I should simply pass as parameters.
1) Do I take ALL parameters and return values of functions I am converting from C to C++ and make them private members?
2) If I don't have to make private members of all parameters and returned values of functions, what criteria do I use to determine which parameters should be privatized to the class versus those which can simply be supplied externally? e.g, cTest.SetVals(4,5), where the variables set to 4 and 5 respectively are not necessarily member variables in the sense that they are only defined in the context of the function, not as stand alone variables in the class.
3) Is my approach wrong to treat the member variables as global variables to the member function as in the example below: mVal, mDrop and mDun were defined as private variable members but they are not passed as parameters but simply show up as global references.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
//drop => drop value. dun: //0 = MOA, 1 = MILs
void AS1113::SetMOA(){
float val;
unsigned char step;
#define MAX_POS 3 //supports a maximum MOA 2*3 = 6
if(mDun) //If in mils
val = mDrop * MIL_CONVT; //convert to MOA
else {val = mDrop;}
step = (unsigned char)floor((MAX_POS - (val/MOA_RESOLUTION)) + 0.5);
mVal = TRAY >> step;
WriteLED();
}
|
4) If you are not supposed to use these member variables globally, is the only way to pass these values by self-referencing the class, as in the example below? By self-referencing, I mean that the class basically has to pass an object created from itself as a parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
void AS1113::WriteLED(AS1113& a){
int temp;
//Clock in LSB First
gpio_set_gpio_pin (OEN);
for(short x=0; x < LED_CYCLES; x++){
temp = a.mVal & LSB_MASK;
if (temp)
gpio_set_gpio_pin (SDI);
else
{gpio_clr_gpio_pin (SDI);}
cpu_delay_us(1, FOSC0);
gpio_set_gpio_pin (SCK);
cpu_delay_us(1, FOSC0);
gpio_clr_gpio_pin (SCK);
a.mVal = a.mVal >> 1;
}//end for
gpio_clr_gpio_pin (SDI);
//Pulse LOAD HIGH for 1us
cpu_delay_us(1, FOSC0);
gpio_set_gpio_pin (LOAD);
cpu_delay_us(1, FOSC0);
gpio_clr_gpio_pin (LOAD);
//Pulse OE LOW for 1us
cpu_delay_us(1, FOSC0);
gpio_clr_gpio_pin (OEN);
}
|