代码之家  ›  专栏  ›  技术社区  ›  Anycorn

模板类的typedef声明

  •  3
  • Anycorn  · 技术社区  · 15 年前

    template<typename T>
    struct matrix {
        typedef matrix self_type;    // or
        typedef matrix<T> self_type;
    };
    

    谢谢您

    2 回复  |  直到 15 年前
        1
  •  6
  •   Alexandre C.    15 年前

    在这种特殊情况下(在类模板中), matrix matrix<T>

    请注意,还可以缩写方法参数:

    template <typename T>
    struct matrix
    {
        typedef matrix my_type;
        matrix(); // constructor is abbreviated too
        matrix& operator=(matrix);
    };
    
    // Method argument types can be abbreviated too
    // but not result types.
    template <typename T>
    matrix<T>& matrix<T>::operator=(matrix m)
    {
        // ...
    }
    
        2
  •  0
  •   Scharron    15 年前

    它们都指同一类型。 我想(取决于编译器的实现),matrix单独引用的是类本身,因此它不需要来自环境的任何东西。matrix是指由T(即matrix)模板化的类matrix,因此可能需要编译器提供更多的演绎机制。 但我只是猜测,我从来没有读过关于这个的任何东西。