Next code presents class-parameter-wrapper, it allows to access underlying class via getters and setters. Simplified version:
template<typename T>
class Parameter
{
public:
typedef T value_type;
typedef Parameter<T> Type;
protected:
T value_;
public:
Parameter() { this->value_ = this->default_value(); }
Parameter(T&& val) { this->set(std::forward<T>(val)); }
Parameter(const T& val) { this->set(std::forward<T>(val)); }
virtual ~Parameter() {};
// Get
T& get() { return this->value_/*this->getter()*/; }
operator T&() { return this->get(); }
// Set
Type& set(T&& val)
{
std::cout << "T&& setter called with " << val << std::endl;
value_ = this->setter(std::forward<T>(val));
return *this;
}
Type& set(const T& val)
{
std::cout << "constT& setter called with " << val << std::endl;
value_ = this->setter(std::forward<const T>(val));
return *this;
}
Type& operator=(T const& val) { return this->set(val); }
Type& operator=(T&& val) { return this->set(val); }
virtual T setter(T const& val) { return val; }
virtual const T& default_value()
{
static const T default_value_{};
return default_value_;
};
};
Using:
int main()
{
struct IncrementorPar : Parameter<int>
{
using Parameter::Parameter; //todo HIDE
using Parameter::operator=; //todo HIDE
virtual int setter(int const& val) { return val + 1; }
virtual const int& default_value(){ return -1; };
} in1(1), in2 = 2, int0;
//assert(int0==int0.default_value()); //FAIL
//assert(int0==-1); //FAIL
//assert(in1 == 2); //FAIL
//assert(in2 == 3); //FAIL
auto *pi1 = new IncrementorPar(2);
//assert(*pi1==3); //FAIL
pi1->set(2);
assert(*pi1==3);
*pi1 = 33;}
}
How could it be possible to call descendant's methods setter()
and default_value()
from ancestor's constructor?
How can I hide using
's?
Aucun commentaire:
Enregistrer un commentaire