简体   繁体   中英

Pointer/reference *& operator overload

I am new using c++ and browsing some source code I found that code in a class.

SDL_Surface *m_srf;
//...
operator SDL_Surface*&()
{
    return m_srf;
}

It overloads both pointer (*) and reference or mem adress (&) operators?

It's a conversion operator. It performs conversions to the type SDL_Surface*& , id est, the type of references to pointers to SDL_Surface .

它是将对象转换为对SDL_Surface指针的引用的转换。

That's a conversion operator : a member operator called Class::operator Type() can be used to convert an object of type Class to an object of type Type .

In this case, it converts to a reference to a pointer to SDL_Surface . So you can use this class wherever that type is required:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM