在泛型类中实现特定方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在泛型类中实现特定方法相关的知识,希望对你有一定的参考价值。
我有一个问题,在互联网上搜索了一段时间,但没有什么好消息出来。
我有一个用于2D图片的通用类Image2D
:
template <typename TValue>
class Image2D
{
public:
typedef Image2D<TValue> Self; // le type de *this
typedef TValue Value; // le type pour la valeur des pixels
typedef std::vector<Value> Container; // le type pour stocker les valeurs des pixels de l'image.
Image2D( int w, int h, Value g = Value() )
: m_width(w), m_height(h), m_data(Container(w * h, g)) { }
struct Iterator : public Container::iterator
{
Iterator( Self & image, int x, int y )
: Container::iterator( image.m_data.begin() + image.index( x, y ) ) { }
};
Iterator begin()
{
return start( 0, 0 );
}
Iterator end()
{
return start( 0, h() );
}
Iterator start( int x, int y )
{
return Iterator( *this, x, y );
}
...
};
它使我能够为图片的像素选择特定的类型,例如当我实例化该泛型类时unsigned char
或Color(unsigned char, unsigned char, unsigned char)
。
我需要向该类添加一个方法,其中实现可能因某些类型而异:
template <typename TValue>
class Image2D
{
...
template <typename Color>
void sepiaFilter()
{
for(Iterator it = this -> begin(), itE = this -> end(); it != itE; ++it)
{
Color oldColor = *it;
Color newColor = oldColor;
newColor.red = std::min((oldColor.red * .393) + (oldColor.green * .769) + (oldColor.blue * .189), 255.0);
newColor.green = std::min((oldColor.red * .349) + (oldColor.green * .686) + (oldColor.blue * .168), 255.0);
newColor.blue = std::min((oldColor.red * .272) + (oldColor.green * .534) + (oldColor.blue * .131), 255.0);
*it = newColor;
}
}
...
};
这同样适用于unsigned char
,但该方法的核心不应该是相同的。
问题是我不知道如何为特定类型专门化泛型函数。我试图创建这个:
template<>
class Image2D<Color>
{
template <typename Color>
void sepiaFilter()
{
for(Iterator it = this -> begin(), itE = this -> end(); it != itE; ++it)
{
Color oldColor = *it;
Color newColor = oldColor;
newColor.red = std::min((oldColor.red * .393) + (oldColor.green * .769) + (oldColor.blue * .189), 255.0);
newColor.green = std::min((oldColor.red * .349) + (oldColor.green * .686) + (oldColor.blue * .168), 255.0);
newColor.blue = std::min((oldColor.red * .272) + (oldColor.green * .534) + (oldColor.blue * .131), 255.0);
*it = newColor;
}
}
}
并创建另一个特定类的Image2D。但这样做需要迭代器必须在该专门类中重新实现;所以我不能使用泛型类'迭代器。
所以这些解决方案都不起作用所以我正在寻求帮助.. Heeeeelp!
我怎么能做我想要的?
如果我理解你的问题,那么你可以采取一些路线。我认为你想要的是模板专业化。为此,您需要修改泛型类以包含要专门化的函数:
template <typename TValue>
class Image2D{
//your original code here
void sepiaFilter(Image2D<Value> img){}
};
请注意,我没有给函数一个正文。现在它什么也没做。现在我们专门研究这个功能:
template<>
void Image2D<Color>::sepiaFilter(Image2D<Color> img){
//body of your function here
}
就是这样! Image2D<Color>
现在有特别定义的行为。
此外,这不是你的问题的一部分,而是对你的足迹来说明这个功能。我会建议改为使用
//code before function definition
static void sepiaFilter(Image2D<Value>& img){
//modify member values of img by reference
}
//code after function definition
添加static
是因为该函数不会修改其自身对象的成员值。并且&符号通过引用传递,否则当你完成时,你对img
所做的任何改变都会消失。或者,你可以这样做:
//code before function definition
void sepiaFilter(){
//modify member values of this
}
//code after function definition
在这种情况下,您可能希望修改自己的成员而不是传递的成员
无论如何,我希望这能回答你的问题,祝你好运!
谢谢你们的帮助! Bronicki的解决方案正在发挥作用。做完他说的用之后
void sepiaFilter() { ... }
是个很好的解决方案。
我打电话给它
img.sepiaFilter();
在main方法中,img是一个Image2D实例。
以上是关于在泛型类中实现特定方法的主要内容,如果未能解决你的问题,请参考以下文章