0

My class needs to call the constructor of his parent class. But I am having issues with that, due to the fact that they are template classes.

The code of my derived class is given below:

template <class CTHIS, typename T>
class genericalMethodClassWithSeveralInternalParametersClass:public genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>
{
    protected:
        float (CTHIS::*funcVersionWith2Parameters)(float, myNUpleType<T>) const;
    public:
        genericalMethodClassWithSeveralInternalParametersClass(float (CTHIS::*_funcVersionWith2Parameters)(float, myNUpleType<T>) const, const CTHIS *_thisPar, myNUpleType<T> _fixedParam)
            {
                funcVersionWith2Parameters = _funcVersionWith2Parameters;
                genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>::genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>(funcVersionWith2Parameters, _thisPar, _fixedParam); // compiler doesn't accept this line
            }
};

The second line of the constructor is supposed to call the constructor of the parent class, but the compiler doesn't accept it. What should I put in there instead?

EDITED: I've simply added the constructor in the initializer list and it worked:

template <class CTHIS, typename T>
class genericalMethodClassWithSeveralInternalParametersClass:public genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>
{
    protected:
        float (CTHIS::*funcVersionWith2Parameters)(float, myNUpleType<T>) const;
    public:
        genericalMethodClassWithSeveralInternalParametersClass(float (CTHIS::*_funcVersionWith2Parameters)(float, myNUpleType<T>) const, const CTHIS *_thisPar, myNUpleType<T> _fixedParam):genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>(_funcVersionWith2Parameters, _thisPar, _fixedParam)
            {
                funcVersionWith2Parameters = _funcVersionWith2Parameters;
            }
};

Still, for the future, I would like to know how could I call the constructor of the parent inside of the derived class code. Actually, thinking for a while, this syntax issue would probably appear at any call for method of a parent class (not just the constructor).

2 Answers2

2

The only place where you can call a base class constructor is in the constructor initializer list:

genericalMethodClassWithSeveralInternalParametersClass(float (CTHIS::*_funcVersionWith2Paramaters)(float, myNUpleType<T>) const, const CTHIS *_thisPar, myNUpleType<T> _fixedParam)
        :genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>(&_funcVersionWith2Paramaters, _thisPar, _fixedParam)
            {
                funcVersionWith2Paramaters = _funcVersionWith2Paramaters;   
            }

Example on godbolt.

Note that the base class portion of a class always must be constructed before the class itself. So you cannot first initialize members of the derived class, and then go on to construct the base after.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • "Note that the base class portion of a class always must be constructed before the class itsel". Is it a rule for classes with templates? Because I have always used this for regular classes. Calling parentClass::parentClass() from inside myDerivedClass::myDerivedClass has always worked for me at situations in which I needed to do something at some internal variable before calling the parentClass constructor. – Anderson Brasil Apr 11 '21 at 07:38
  • 1
    @AndersonBrasil No this is a general rule and applies also to non-template classes. See [this question](https://stackoverflow.com/questions/21395395/calling-a-constructor-of-the-base-class-from-a-subclass-constructor-body) for why your code compiled in the other cases and why that code was not doing what you thought it did. – ComicSansMS Apr 11 '21 at 08:07
  • Nice, I learned something. I wonder how my programs so far worked well with all my constructors written the wrong way lol. – Anderson Brasil Apr 11 '21 at 22:16
1

The easy way is to make a typedef for the base type.

using base_type = genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType>;

then the constructor becomes:

genericalMethodClassWithSeveralInternalParametersClass(float (CTHIS::*_funcVersionWith2Paramaters)(float, myNUpleType<T>) const, const CTHIS *_thisPar, myNUpleType<T> _fixedParam)
: base_type(&_funcVersionWith2Parameters, _thisPar, _fixedParam),
funcVersionWith2Paramaters (_funcVersionWith2Paramaters)
            {
             }
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23