C++ 使用模板在运算符中实现
我想定义T&operator()(int x,int y)函数,但我不知道如何定义它。 我在Array.h文件中有这个函数,所以我想我必须在Array.hpp文件中定义它。有人知道吗C++ 使用模板在运算符中实现,c++,templates,operators,C++,Templates,Operators,我想定义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 <t
#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
\ifndef\u数组_
#定义_数组_
名称空间数学
{
/*!Array类实现T类型元素的通用二维数组。
*/
模板
类数组
{
受保护的:
//!T型数组元素的平面存储
T*缓冲器;
无符号整数宽度,
高度;
/*返回对零基位置(x列,y行)元素的引用。
*
*\param x是数组的从零开始的列索引。
*\param y是数组的从零开始的行索引。
*
*\返回对位置(x,y)处元素的引用
*/
T&运算符()(int x,int y);
};
}//名称空间数学
#包括“Array.hpp”
#恩迪夫
例如,像这样
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
}
};
模板
类数组
{
受保护的:
//!T型数组元素的平面存储
T*缓冲器;
公众:
无符号整数宽度、高度;
///非常量元素访问
///\param[in]x是数组的从零开始的列索引。
///\param[in]y是数组的从零开始的行索引。
///\返回对位置(x,y)处元素的引用
T运算符()(整数x,整数y)
{
返回(缓冲区+x*高度)[y];
}
///常量元素访问
///\param[in]x是数组的从零开始的列索引。
///\param[in]y是数组的从零开始的行索引。
///\返回位置(x,y)处元素的常量引用
常量和运算符()(整数x,整数y)常量
{
return const_cast(*this)(x,y);//注意:重复使用上面的
}
};
您是否在问如何定义它?或者如何实现它?@doctorlove我想问一下如何定义它,谢谢