#include <Arduino.h>
class foo {
public:
int bar1 = 0;
int bar2 = 0;
int bar3 = 0;
foo(int foo1,int foo2,int foo3) //all variables initialised during creation
{
bar1=foo1;
bar2=foo2;
bar3=foo3;
};
void beginFoo(int foo1,int foo2,int foo3) //all variables initialised in setup()
{
bar1=foo1;
bar2=foo2;
bar3=foo3;
};
void setBar1(int param1)// set methods to setup variables
{
bar1 = param1;
};
};
foo foobar(1, 2, 3);//instantiate object
void setup() {
//foobar.beginFoo(5, 6, 7);
}
void loop() {
Serial.begin(9600);
delay(1000);
foobar.setBar1(4);
Serial.println(foobar.bar1);
Serial.println(foobar.bar2);
Serial.println(foobar.bar3);
while (1)
{
delay(100);
}
}
The above is for a hook for discussion. I will have a class which has many public parameters - its for plotting a graph so there are quite a few natural things that need to be set like colours, axes limits, enables for tick marks, etc. I've done my best to divide out things into sub structures (like axes), but the main graph object still has a fair number of parameters.
My question is: when the parameter list is long, what's the best practice for initialising a class instance?
Above there are variables defined in the constructor; variables defined in an initialiser; and set methods for each variable (OK I only put one in). It seems to me that set methods are cumbersome and produce a lot of code waffle, but the other options mean that all parameters must be defined together resulting in inefficiency. Is there a middle ground?
Then the other thing is the naming of parameters. Internally the variables of the class have one name and in the method definitions they have another name - is this always necessary - do you have to have two names for everything, in essence?
Please excuse my lack of OO experience - I'm trying to learn how to do it properly
I was brought up on assembler and BASIC, with a splash of Algol ..
For a complex object like that it's probably a combination - set the critical parameters in the constructor and provide setter/getter functions to allow control of the others.
Begin functions are usually used when the object touches hardware that should be initialized before the object is used. Constructors run before init, so you need something you can call later.
And then there is the virtual insanity with C++ constructors; A derived class will not execute the constructor of its base class unless explicitly told to. Using the initializer list is AFAIR the only way of doing this, so I would opt for using the initializer list for member initialization and put any init code inside the curlies.