我如何使用“operator X()”类中定义?

发布于 2024-12-13 19:31:56 字数 520 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无力看清 2024-12-20 19:31:56

您只需在需要 SC_HANDLE 的表达式中使用 ServiceHandle 类型的对象即可。您所讨论的运算符SC_HANDLE的转换运算符。该运算符会自动“使用”。

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.

半暖夏伤 2024-12-20 19:31:56

那是一个铸造操作员。这将称之为:

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

That is a casting operator. This would call it:

ServiceHandle s(some handle);
SC_HANDLE h = (SC_HANDLE)s;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文