Passing parameters to constructor - very obvious and basic question from an OF
-
I am rebuilding my application and would like NOT to make local copies of parameters passed to the class constructor. I have few methods which used to access application global variables , they are actually hardware "constants", and do not want to do that anymore. I can rebuild these methods to include constructor passed parameters. no issue there. But I was wondering if there is an another way to access the constructor parameters without adding them to the methods parameters or making local copies. I hope I have explained my dilemma , if not , sorry. Thanks Vaclav ( C/C++ and gcc)
-
I am rebuilding my application and would like NOT to make local copies of parameters passed to the class constructor. I have few methods which used to access application global variables , they are actually hardware "constants", and do not want to do that anymore. I can rebuild these methods to include constructor passed parameters. no issue there. But I was wondering if there is an another way to access the constructor parameters without adding them to the methods parameters or making local copies. I hope I have explained my dilemma , if not , sorry. Thanks Vaclav ( C/C++ and gcc)
-
I am rebuilding my application and would like NOT to make local copies of parameters passed to the class constructor. I have few methods which used to access application global variables , they are actually hardware "constants", and do not want to do that anymore. I can rebuild these methods to include constructor passed parameters. no issue there. But I was wondering if there is an another way to access the constructor parameters without adding them to the methods parameters or making local copies. I hope I have explained my dilemma , if not , sorry. Thanks Vaclav ( C/C++ and gcc)
It is normal practice to add parameters that remain constant in the object to the constructor like:
class Foo
{
private:
int paramThatDoesNotChangeOften;
public:
Foo(int someParam)
{
paramThatDoesNotChangeOften = someParam);
}
};You could then add a getter method to return the value to the user if it's value is needed outside the class. Is that what you are querying?
-
It is normal practice to add parameters that remain constant in the object to the constructor like:
class Foo
{
private:
int paramThatDoesNotChangeOften;
public:
Foo(int someParam)
{
paramThatDoesNotChangeOften = someParam);
}
};You could then add a getter method to return the value to the user if it's value is needed outside the class. Is that what you are querying?
Thanks for replies. It may be stupid, but that is what I am trying to avoid. Now I would have "someParam" - actually global array and a class copy of the same so it can be accessed by class methods. I am trying to save some memory of embedded processor. Kinda of wishful thinking ahead. I won't get to upset if I need to make a local copies. Would using "this" pointer work making local copy? I just tryied "standard" this.name = name but could not make it work on pointers. I guess I am still struggling with using pointers. Addendum I have decided to KISS and I'll be making copies of the parameters using "this" pointer. One question remains - since all of the parameaters passed are de facto constants - harwdware pins, LCD size etc. and won't be modified by the class, why can't I just use "this>LCD_x_size" as variable instead of "this->LCD_x_size = LCD_x_size" Now the class local varaible is "LCD_x_size". And yes, I read somewhere that using same symbols for "name" and "variable" is OK. Confusing, but OK.
-
I am rebuilding my application and would like NOT to make local copies of parameters passed to the class constructor. I have few methods which used to access application global variables , they are actually hardware "constants", and do not want to do that anymore. I can rebuild these methods to include constructor passed parameters. no issue there. But I was wondering if there is an another way to access the constructor parameters without adding them to the methods parameters or making local copies. I hope I have explained my dilemma , if not , sorry. Thanks Vaclav ( C/C++ and gcc)
If the parameters are actually constants, how about instantiating a template class?
int const PROCESSOR_COUNT = 42;
template scheduler
{
// constructors, other methodsprivate:
processor_data data[processor_count];
};scheduler sched;
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill