开发者

PREDICATE( name, arity )

开发者 https://www.devze.com 2022-12-29 10:04 出处:网络
Suppose I have the program below: #include files PREDICATE( add, 3 ) { return A3 = (long)A1 + (long)A2; } int main( int argc, char** argv )

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 li开发者_运维问答sting 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,


If it is necessary to call external code on private members of classes, then those classes need to call external code. The code must be "C" not "C++" compiled, in order to preserve symbol names.

In this case the class with the private x,y would have to call add(x,y) and get the result. The class needs a method to tell it to call the external add/3.

 extern "C" int add(int x, int y);
 class Priv{
        int x,y;
      public:
        int privadd(void){
            return add(x,y);
        }
   }; 
0

精彩评论

暂无评论...
验证码 换一张
取 消