Parameter types substitute the basic types for a more convenient templatised structure when used within a class. Ideally the only way any member variable should be modified is via member functions of the class itself. When you start allowing member variables to be extracted and modified outside a class, you inevitably lose control of the class (and in practice this happens by lazy/tired programmers more often than not) and bugs start creeping in.
The usual way to implement this is with get and set methods providing access. However, this can get somewhat verbose for simple classes:
class A { public: A() {}; int getCounter() const { return m_counter; } void setCounter (const int& counter) { m_counter = counter; } private: int m_counter; };
This can be simplified with the use of templates and the () operator resulting in the Parameter class defined here.
Include the following at the top of any translation unit that requires compilation of class that uses parameters.
#include <ecl/utilities.hpp> using ecl::Parameter;
Since it is a template class, no linking is required if you are only using this class.
The parameter implementation of a class A (compare with the set/get implementation described earlier) looks like:
class A { public: A() {};
Parameter<int> counter; }; Here access to the variable is done very similarly (but without the verbosity of set/get).
int main() { A a; a.counter(1); // <-- Sets the variable cout << a.counter(); << endl; // <-- Gets the variable }
Note that even though Parameter<int> is defined publicly, the contents are still privately allocated, but here they are tucked away in Parameter's internal implementation.
There are also convenience methods if the above usage is undesirable.
a.counter = 1;
int a_copy = a.counter;
The difference between these and public variables is that these can never be set outside of the class. The reason - regualar public variables can be hooked by a pointer/reference and then passed off to another function where the variable can be modified...well outside of the control of the original class. In this case here, the only hooks that can be made to the variable are const pointers or const references. Thus, even though the notation is the same, these parameter variables are protected by their constness.