我如何使用“operator X()”类中定义?
我对 C++ 比较陌生,这个声明让我感到困惑:
Service.h:
class ServiceHandle {
public:
ServiceHandle(SC_HANDLE h) : handle(h) {}
...
operator SC_HANDLE() const {return handle;}
protected:
SC_HANDLE handle;
};
我通过此处列出的构造函数之外的其他方式创建了一个 ServiceHandle
对象。我想将实际的 SC_HANDLE
传递给 ChangeServiceConfig
,如何获取它?我假设这与操作员有关,但我不知道如何使用它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需在需要
SC_HANDLE
的表达式中使用ServiceHandle
类型的对象即可。您所讨论的运算符是SC_HANDLE
的转换运算符。该运算符会自动“使用”。You just use the object of type
ServiceHandle
in the expression tht expectsSC_HANDLE
. The operator you're talking about is the casting operator toSC_HANDLE
. This operator is "used" automatically.那是一个铸造操作员。这将称之为:
That is a casting operator. This would call it: