Implementing a non template method defined in a template class

前端 未结 5 843
盖世英雄少女心
盖世英雄少女心 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条回答
  • 2021-01-04 08:45

    You'll need to define your method like this:

    template class<typename T>
    void MyClass<T>::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.

    0 讨论(0)
  • 2021-01-04 08:47

    Well, if the method doesn't depend on template parameter, you can only do that with inheritance AFAIK.

    The downside: more code + inheritance

    The upside: (much) less code generated, depending on what parts of your code actually are template dependent. In the below example, method NonDependentMethod will only generate one assembly while DependentMethod will generate as many as there are different template parameters (just one in this case, but make a MyClass<float> and you have two, etc).

    #include <iostream>
    using namespace std;
    
    class MyClassBase
    {
    public:
        void NonDependentMethod();
    };
    
    template <class T> class MyClass : public MyClassBase
    {
    public:
        void DependentMethod(T param);
    };
    
    void MyClassBase::NonDependentMethod()
    {
        cout << "NonDependentMethod << endl;
    }
    
    template<class T> void MyClass<T>::DependentMethod(T param)
    {
        cout << "DependentMethod " << param << endl;
    }
    
    int main() {
        // your code goes here
        MyClass<int> cls;
        cls.NonDependentMethod();
        cls.DependentMethod(2);
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-04 08:48

    You need to do it this way:

    template class<typename T>
    void MyClass<T>::myMethod()
    {
       ...
    }
    

    It's not the method that is templated, it's the class.

    You can have a templated method in a non-templated class, a non-templated method in a templated class (your case) and a templated method in a templated class, and of course a non-templated method in a non-templated class.

    0 讨论(0)
  • 2021-01-04 08:54

    Put it in the header file.

    The member function is still a member of the class template, and you'd have to write:

    template <typename T> void MyClass<T>::myMethod() { /* ... */ }
    

    As with all template member functions, this isn't actually a real function yet; it only generates a real function when the class template is instantiated. So the full template definitions have to be visible to everyone who instantiates the template, and the usual way of doing this is by putting everything in the header.

    (Note that member functions of class templates are themselves considered function templates, and you can actually specialize them: template <> void MyClass<int>::myMethod() { }.)

    0 讨论(0)
  • 2021-01-04 09:00

    You have to define it in an even other way. The method itself may not (directly) depend on the template parameter, but the class to which it belongs certaily does, no? As such, the method indirectly depends on the template parameter too:

    template class<typename T>
    void MyClass<T>::myMethod()
    {  //       ^^^ -- note
       ...
    }
    
    0 讨论(0)
提交回复
热议问题