C plus plus template inheritance



template <class Type>
class counter
{
public : counter(Type N=0) { data = N; }
void increment(Type D=1) { data += D; }
};

template <class Type>
class general_counter : public counter<Type>
{
public:
/* This line */
general_counter(Type N=0) : counter(N) { }
void setcounter(Type N=0) { data = N; }
}

Can we initialize the variable that is argument to a constructor, does it have any specific purposes? Why do we call the constructor of the base template class in the inherited class?
Can we initialize the variable that is argument to a constructor, does it have any specific purposes?


I don't understand what you mean by this.

Why do we call the constructor of the base template class in the inherited class?


It works the same way for all inheritance, not just template inheritance. Derived classes call the base class(es) constructor.

If you aren't sure *why* this needs to occur, you should re-learn about inheritance.
It is called that to pass argument N that is specified in the derived constructor to the base constractor that to initialize the data member data of the base class. Otherwise if the explicit call will not be specified the base class will be initialized with implicit call base( 0 ) ( because the default argument is equal to 0 ).
Topic archived. No new replies allowed.