kyb kyb - 1 month ago 7
C++ Question

Call descendant's method from ancestor's methods and constructor

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?

Answer

Not really an elegant solution but...

You could postpone the initialization of value_ using a init() function.

Something like

template<typename T>
class Parameter
{
private:
    bool  toInit { true };
    bool  initWithVal;
    T     valInit;

    void init ()
     {
       if ( initWithVal )
          this->set(valInit);
       else
          value_ = this->default_value();

       toInit = false;
     }

public:
    typedef T value_type;
    typedef Parameter<T> Type;
protected:
    T value_;
public:
    Parameter() : initWithVal{false} { }
    Parameter(T&& val) : initWithVal{true}, valInit{std::move(val)} { }
    Parameter(const T& val) { this->set(std::forward<T>(val)); }
    virtual ~Parameter() {};

    // Get
    T& get() { if ( toInit ) init(); return this->value_/*this->getter()*/; }
    operator T&() { return this->get(); }

    // Set
    Type& set(T&& val)
    {
      toInit = false;
      std::cout << "T&& setter called with " << val << std::endl;
      value_ = this->setter(std::forward<T>(val));
      return *this;
    }
    Type& set(const T& val)
    {
      toInit = false;
      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()
    {
      std::cout << "base default value\n";
        static const T default_value_{};
        return default_value_;
    };
};