【问题标题】:Returning c++ pointers to perl将 c++ 指针返回到 perl
【发布时间】:2011-06-08 08:42:18
【问题描述】:

我在 C++ 中有一个函数,例如:

void* getField(interface* p)   
{  
  int* temp = new int( p->intValue);  
  cout<< "pointer value in c++" << temp << "value of temp = " << temp << endl;  
  return temp;  
}  

我正在使用 SWIG 为上述类生成包装器。现在我试图在 Perl 中获取返回的指针值。我该怎么做??
我编写了以下 perl 脚本来调用该函数:

 perl  
 use module;  
 $a = module::create_new_interface();  
 $b = module::getField();  
 print $b,"\n";  
 print $$b, "\n";  

我已经正确定义了 create_interface 函数,因为在调用函数 getField() 时会打印接口的正确 intValue。

程序的输出如下:

_p_void=SCALAR(0x5271b0)
5970832
pointer value in c++ 0x5b1b90 value of temp 22

为什么这两个值——C++ 中的指针和 perl 中的引用不同?我如何从参考中获取 intValue? (即值 22 )

【问题讨论】:

    标签: perl swig


    【解决方案1】:

    因为您打印了一个十进制和一个十六进制:

     printf "pointer is 0x%x\n", $$b;   # prints "pointer is 0x5b1b90"
    

    Perl 通常不使用指针。 packunpack 函数可以处理指针(参见 Packing and Unpacking C Structures),但正常的 Perl 习惯用法是让函数返回一个整数而不是指向整数的指针。

    【讨论】:

      猜你喜欢
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 2012-08-01
      • 2011-02-19
      • 2017-08-22
      • 1970-01-01
      相关资源
      最近更新 更多