mercredi 3 octobre 2018

How to initialize members of template-base class from derived constructor initializer list?

This:

template <typename T>
struct base {
    T a;
};

template <typename T>
struct derived : base<T> {
    derived(T v) : a(v) {} // xxx: how?
};

int main() {
    return 0;
}

Goes boom:

test.cc: In constructor ‘derived<T>::derived(T)’:
test.cc:12:20: error: class ‘derived<T>’ does not have any field named ‘a’
     derived(T v) : a(v) {} // xxx: how?

If I replace the a(v) with { this->a = v; } it's fine, is there any way to initialize members of a templated base class from the initializer list of a derived class?

Aucun commentaire:

Enregistrer un commentaire