0

Suppose I have the program below:

#include files

PREDICATE( add, 3 )
{
    return A3 = (long)A1 + (long)A2;
}

int main( int argc, char** argv )
{
    PlEngine e( argv[0] );
    PlCall( "consult('myFile.pl')" );
    PL_halt( PL_toplevel() ? 0 : 1 );
}

When I compile it, it links Prolog and C++ and then launches the Prolog command prompt.

All I have in myFile.pl is

:- use_module( library(shlib) ).

When I type listing at the Prolog prompt, I get

Foreign: add/3

My question is how do I use the result of some other subroutine, say a class, in my foreign predicate add? Let's say I have a class somewhere in my program that calculates some x and y. Obviously x and y would be private or protected members of that class' header file. How do I use x and y in my add predicate? For instance, if I wanted to return the sum of x and y and first and second arguments of add?

Cheers,

4

1 回答 1

0

如果需要在类的私有成员上调用外部代码,那么这些类需要调用外部代码。为了保留符号名称,代码必须是“C”而不是“C++”编译的。

在这种情况下,具有私有 x,y 的类必须调用 add(x,y) 并获得结果。该类需要一个方法来告诉它调用 external add/3

 extern "C" int add(int x, int y);
 class Priv{
        int x,y;
      public:
        int privadd(void){
            return add(x,y);
        }
   }; 
于 2010-10-22T00:35:06.123 回答