2016-12-17 4 views
-1

T & operator()(int x、int y)関数の定義方法を理解できません。 私はArray.hファイルにこの機能を持っています。だから私はそれをArray.hppファイルに定義する必要があります。誰も考えていますか?テンプレートを使用した演算子での実装

#ifndef _ARRAY_ 
#define _ARRAY_ 

namespace math 
{ 

/*! The Array class implements a generic two-dimensional array of elements of type T. 
*/ 
    template <typename T> 
    class Array 
    { 
    protected: 
    //! Flat storage of the elements of the array of type T 
     T * buffer;      
     unsigned int width,   
        height; 
     /* Returns a reference to the element at the zero-based position (column x, row y). 
     * 
     * \param x is the zero-based column index of the array. 
     * \param y is the zero-based row index of the array. 
     * 
     * \return a reference to the element at position (x,y) 
     */ 
     T & operator() (int x, int y); 

     }; 
    } // namespace math 

#include "Array.hpp" 
#endif 
+0

アウトラインで定義する方法をお尋ねしますか?またはそれを実装する方法? – doctorlove

+0

@doctorlove私はそれを定義する方法を尋ねます、ありがとう – madrugadas25845

答えて

0

このようにしてください。

template <typename T> 
class Array 
{ 
protected: 
    //! Flat storage of the elements of the array of type T 
    T * buffer; 
public:      
    unsigned int width, height; 
    /// non-constant element access 
    /// \param[in] x is the zero-based column index of the array. 
    /// \param[in] y is the zero-based row index of the array. 
    /// \return a reference to the element at position (x,y) 
    T & operator() (int x, int y) 
    { 
    return (buffer+x*height)[y]; 
    } 
    /// constant element access 
    /// \param[in] x is the zero-based column index of the array. 
    /// \param[in] y is the zero-based row index of the array. 
    /// \return a const reference to the element at position (x,y) 
    T const & operator() (int x, int y) const 
    { 
    return const_cast<Array&>(*this)(x,y); // note: re-use the above 
    } 
}; 
関連する問題