0

Unfortunately, I have faced a challenge since I can't use abstraction and templates simultaneously. The idea is to keep the parent class as general as possible so we put the general form of two member functions as an abstraction in the parent class. Then, I wanted to define these functions in the child class how I wanted them to be. But the problem arises when I want to keep the parent template not even after the declaration of child class not make it as soon as I go to the child class. Please someone show me a solution if there are any?

template <class y2>
class parrennt
{
    public:
    y2 numb;
    virtual y2 funcc3()=0;
    virtual y2 funcc4()=0;

};

template <class y2>
class chilld : public parrennt <double> // I want to keep it <class y2> if its possible
{
    public:
    virtual y2 funcc3 () { return numb*2;}
    virtual y2 funcc4 () { return numb*3;}

};


NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Msain
  • 1

1 Answers1

0

There are some tricky parts in the name lookup of base class members - especially when the base class depends on the template parameter, like here. Templates have two-phase name lookup: first when the template is compiled, and then for dependent names an additional lookup once template parameters are known.

numb is not obviously dependent on y2, so it will be looked up in class chilld only.

The solution is very simple. Write this->numb. Since this has type child<y2>*, it is dependent on y2, and the name lookup of this->numb is delayed till the second phase where y2 is known, and also parent<y2>

MSalters
  • 173,980
  • 10
  • 155
  • 350