lundi 2 janvier 2017

How to access child member from parent's template function?

#include <iostream>
#include <functional>

using namespace std;

class Child;

class Parent {
    public:
    template <class Function, class... Args>
    void f(Function&& f, Args&&... args)
    {
        Child *c = dynamic_cast<Child*>(this);
        cout << c->n;
    }
};

class Child : public Parent {

public:
    int n = 0;
};

int main()
{
    Parent *p = new Child();
    cout << "abc";
    return 0;
}

The code intends to access child class's member from parent's template member function. I'd like to do this because the template member function cannot be virtual. The error I got is: "'Child' is an incomplete type". How do I make this work?

Aucun commentaire:

Enregistrer un commentaire