fredley
fredley

Reputation: 33871

How can I use "operator X()" defined in class?

I'm relatively new to C++, and this declaration has me confused:

Service.h:

class ServiceHandle {
  public:
    ServiceHandle(SC_HANDLE h) : handle(h) {}
    ...
    operator SC_HANDLE() const {return handle;}
  protected:
    SC_HANDLE handle;
};

I've created a ServiceHandle object through other means than the constructor listed here. I'd like to get the actual SC_HANDLE to pass to ChangeServiceConfig, how do I get at it? I'm assuming it's something to do with the operator, but I can't work out how to use it.

Upvotes: 4

Views: 125

Answers (2)

RobH
RobH

Reputation: 3338

That is a casting operator. This would call it:

ServiceHandle s(some handle);
SC_HANDLE h = (SC_HANDLE)s;

Upvotes: 0

valdo
valdo

Reputation: 12923

You just use the object of type ServiceHandle in the expression tht expects SC_HANDLE. The operator you're talking about is the casting operator to SC_HANDLE. This operator is "used" automatically.

Upvotes: 4

Related Questions