托管c++中的操作符重载
Operator overloading in managed C++
My class定义为:(代码段)
public ref class PixelFormatDescriptor
{
public:
PixelFormatDescriptor();
PixelFormatDescriptor(PIXELFORMATDESCRIPTOR *pfd);
const PIXELFORMATDESCRIPTOR* operator*(System::Drawing::GLSharp::PixelFormatDescriptor ^p)
{
return m_pfd;
}
...
private:
PIXELFORMATDESCRIPTOR *m_pfd;
};
我正在尝试使用它与以下内容:
PixelFormatDescriptor ^pfd = new PixelFormatDescriptor();
::ChoosePixelFormat(m_hdc, pfd);
我的问题是ChoosePixelFormat
期望pfd是const PIXELFORMATDESCRIPTOR *
,我如何修复操作符过载以允许我传递PixelFormatDescriptor ^
并使其自动返回PIXELFORMATDESCRIPTOR *
而无需实现命名属性或Get方法。
下面是定义相同转换操作符的方法,但作为静态方法,我认为这在管理领域更标准。
static operator PIXELFORMATDESCRIPTOR* (PixelFormatDescriptor ^p)
{
return p->m_pfd;
}
下面是记录语法的页面:
http://msdn.microsoft.com/en-US/library/vstudio/047b2c75.aspx我已经浏览了google上的许多页面,发现重载操作符的文档相当缺乏,但我已经找到了答案:
操作符重载应该是
operator const PIXELFORMATDESCRIPTOR*()
{
return m_pfd;
}
我想我应该把答案放在这里,以防有人需要这个答案。