0

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) {}
};
1
  • Both are correct in current example. The first one would be required in case of multiple inheritance (of Association). Commented Apr 19, 2017 at 16:35

1 Answer 1

2

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>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.