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

Duncan Murdoch murdoch at stats.uwo.ca
Mon Mar 17 20:03:27 CET 2008


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



More information about the R-devel mailing list