如何使用 SWIG 返回指向用户定义的类对象的指针

发布于 2024-07-29 22:19:16 字数 451 浏览 6 评论 0 原文

我有以下由 swig 包装的代码:

int
cluster::myController(controller*& _controller) {
   _controller = my_controller;
   return 0;
}

controller 有一个私有构造函数。

使此类抛出异常的正确咒语是什么?

 public static void main(String argv[]) {
    controller c = null;
    int r = dan_cluster.theCluster().myController(c);
    System.out.println(r);
    System.out.println(c.getUUID());
  }

I have the following code wrapped by swig:

int
cluster::myController(controller*& _controller) {
   _controller = my_controller;
   return 0;
}

controller has a private constructor.

What's the correct incantation to make something like this not throw an exception?

 public static void main(String argv[]) {
    controller c = null;
    int r = dan_cluster.theCluster().myController(c);
    System.out.println(r);
    System.out.println(c.getUUID());
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

谜兔 2024-08-05 22:19:17

如果您要发布您收到的异常,这将会很有帮助,但我不相信您尝试做的事情是可能的。 在 C++ 中,您可以通过引用传递指针,从而对传递给函数的指针变量进行更改,但在 Java 中没有等效的东西。 您应该检查生成的包装器,但我的猜测是您的 C++ 代码被包装为以下内容:

int myController(controller _controller) {
   _controller = my_controller;
   return 0;
}

...这显然不会执行您想要的操作,从而在您尝试 getUUID() 时导致 NullPointerException。

假设我是对的,最好的解决办法是 myController() 仅返回一个控制器*。 如果您确实需要整数,请考虑返回 std::pair (注意:包装 stl 的任何部分都需要一些技巧,请参阅 文档)。

It would be helpful if you were to post the exception you're getting, but I don't believe that what you're trying to do is possible. In C++ you can pass a pointer by reference and so make changes to a pointer variable passed to a function, but in Java there is nothing equivalent. You should check the generated wrapper, but my guess is that your C++ code is being wrapped as something like:

int myController(controller _controller) {
   _controller = my_controller;
   return 0;
}

...which clearly won't do what you want, leading to a NullPointerException when you try to getUUID().

Assuming I'm right, the best fix is for myController() to just return a controller*. If you really need the integer, consider returning a std::pair (note: wrapping any part of the stl requires some delicacy, consult the documentation).

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