【问题标题】:Pointer/reference *& operator overload指针/引用 *& 运算符重载
【发布时间】:2013-09-17 15:57:43
【问题描述】:
我是使用 c++ 的新手,浏览了一些源代码,我在一个类中发现了该代码。
SDL_Surface *m_srf;
//...
operator SDL_Surface*&()
{
return m_srf;
}
它重载了指针 (*) 和引用或内存地址 (&) 运算符?
【问题讨论】:
标签:
c++
pointers
reference
overloading
operator-keyword
【解决方案1】:
这是一个转换运算符。它执行到类型SDL_Surface*&、id est、指向SDL_Surface 的指针的引用类型的转换。
【解决方案2】:
这是一个转换运算符:称为Class::operator Type() 的成员运算符可用于将Class 类型的对象转换为Type 类型的对象。
在这种情况下,它转换为对指向SDL_Surface 的指针的引用。因此,您可以在需要该类型的任何地方使用此类:
void set(SDL_Surface*& s) {s = whatever;} // needs a reference
void do_something(SDL_Surface*); // needs a pointer
my_class thingy;
set(thingy); // OK - sets thingy.m_srf
do_something(thingy); // OK - passes thingy.m_srf to the function
【解决方案3】:
这是一种将对象转换为对指向 SDL_Surface 的指针的引用的转换。