Totktonado
Totktonado

Reputation: 5

Inheritance from template: correct constructor

When inheriting from a template in child class constructor is it necessary at a call of the parent constructor to specify template parameter. Code example:

    template<typename TYPE>
class Association
{
public:
    Association(TYPE* object) : m_object(object) {}
private:
    TYPE* m_object;
};

class MyClass
{
};

class AssociationToMyClass : public Association<MyClass>
{
    // is the constructor correct
    AssociationToMyClass(MyClass* object) : Association<MyClass>(object) {}
    // or this one?
    AssociationToMyClass(MyClass* object) : Association(object) {}
};

Upvotes: 0

Views: 53

Answers (1)

aschepler
aschepler

Reputation: 72271

Both are correct. Association<MyClass> is more explicit, but there's an "injected class name" visible for name lookup where Association means the same as Association<MyClass>.

Upvotes: 2

Related Questions