首页 > 代码库 > C++ 类型转换操作与操作符重载 operator type() 与 type operator()
C++ 类型转换操作与操作符重载 operator type() 与 type operator()
类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转变为其他类型值的转
换。转换操作符在类定义体内声明,在保留字 operator 之后跟着转换的目标类型。
class CVImage
{
public :
CVImage();
explicit CVImage(unsigned int width, unsigned int height, unsigned short depth, unsigned short nChannels = 3);
CVImage(CVImage& img);
~CVImage();
void ReleaseImage();
int Resize(unsigned int width, unsigned int height, unsigned short depth, unsigned short nChannels = 3);
operator IplImage*() { return m_image; };
inline IplImage* GetImage() { return m_image; };
private:
IplImage* m_image;
};
先来说下类型转换构造函数:C++中的explicit用来修饰类的构造函数,表明该构造函数是显示的,在调用有参数的构造函数
时需要显式调用:
CVImage cImg = CVImage(640, 480, 8, 1);
运算符重载操作:
IplImage* operator() ()
{
return m_image;
}
C++ 类型转换操作与操作符重载 operator type() 与 type operator()