From murdoch at stats.uwo.ca  Mon Mar 17 20:03:27 2008
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Mon, 17 Mar 2008 15:03:27 -0400
Subject: [Rd] how to get access to C++ Objects
In-Reply-To: <008901c88851$36d0c800$21141c0a@revista2>
References: <008901c88851$36d0c800$21141c0a@revista2>
Message-ID: <47DEC07F.4020906@stats.uwo.ca>

On 3/17/2008 1:06 PM, Adrian Mart?nez Vargas wrote:
>  
> 
> In the "Writing R Extensions" manual appears this example, to get access to C++ function using the R commands:
> 
>  
> 
> R> dyn.load(paste("X", .Platform$dynlib.ext, sep = ""))
> 
> constructor Y
> 
> R> .C("X_main")
> 
> constructor X
> 
> destructor X
> 
> list()
> 
>  
> 
>  
> 
> That gives me access to the function "X_main", but how to get access to methods and properties like X.Z() and X.f, defined in the C++ files: 

You need to write C functions to access them.  For example,

void XdotZ(int* a, int* b) {
   X.Z(*a, *b);
}

all wrapped within your extern "C" block.

Duncan Murdoch


> 
>  
> 
> // X.hh
> 
> class X 
> 
> {
> 
> public: 
> 
>             int f;
> 
> X ();
> 
>  ~X ();
> 
> int Z(int a, int b);
> 
> };
> 
> class Y 
> 
> {
> 
> public: Y ();
> 
>  ~Y ();
> 
> }; 
> 
>  
> 
>  
> 
> // X.cc
> 
> #include <iostream>
> 
> #include "X.hh"
> 
> static Y y;
> 
> X::X() { std::cout << "constructor X" << std::endl; }
> 
> X::~X() { std::cout << "destructor X" << std::endl; }
> 
> Y::Y() { std::cout << "constructor Y" << std::endl; }
> 
> Y::~Y() { std::cout << "destructor Y" << std::endl; }
> 
> X::Z(int a, int b) {return a+b;}
> 
>  
> 
>  
> 
> // X_main.cc:
> 
> #include "X.hh"
> 
> extern "C" 
> 
> {
> 
> void X_main () 
> 
> {
> 
> X x;
> 
> }
> 
> } // extern "C"
> 
> 
> 
> Best regards 
> 
> Dr. Adrian Mart?nez Vargas 
> Revista Miner?a y Geolog?a (Editor Principal) 
> ISMM, Las Coloradas, s/n 
> Moa, Holgu?n, 
> Cuba 
> CP. 83329 
> http://www.ismm.edu.cu/revistamg/index.htm
> 
> 
> 	[[alternative HTML version deleted]]
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] how to get access to C++ Objects
Duncan Murdoch