alhuelamo
alhuelamo

Reputation: 135

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?

Upvotes: 0

Views: 715

Answers (3)

Mike Seymour
Mike Seymour

Reputation: 254631

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

Upvotes: 1

Mats Petersson
Mats Petersson

Reputation: 129454

It is a conversion that convertst the object into a reference to a pointer to SDL_Surface.

Upvotes: 1

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234584

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

Upvotes: 3

Related Questions