Implementing a non template method defined in a template class

前端 未结 5 887
盖世英雄少女心
盖世英雄少女心 2021-01-04 08:14

When I want to define a method declared in a template class, but that the method doesn\'t depend on the template parameters, have I to define it in the include files as :

5条回答
  •  猫巷女王i
    2021-01-04 08:45

    You'll need to define your method like this:

    template class
    void MyClass::myMethod()
    {
        // Method Body
    }
    

    The reason for this is that the method actually is dependent on the template parameter. Remember that every method has access to the special variable this; during the method call this is actually a parameter passed to the method. this changes in type depending on the template parameter specified during object instantiation and, as such, all methods must be template methods in order to accommodate all forms of this.

提交回复
热议问题