<html><body><div style="color:#000; background-color:#fff; font-family:Courier New, courier, monaco, monospace, sans-serif;font-size:12pt"><div><span>All I want to do is obtain a shared-pointer equivalent in R to a C++ object and be able to do two things with it - 1. Invoke methods of the object via the shared-pointer and 2. pass the shared-pointer to the methods of other C++ class objects.</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; background-color: transparent; font-style: normal;"><span><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; background-color: transparent; font-style: normal;"><span>In Python, I am able to do this like so (Boost shared-ptrs are easily passed back and forth across the Python-C++ boundary using Boost-Python):</span></div><div style="color: rgb(0, 0, 0);
 font-size: 16px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; background-color: transparent; font-style: normal;"><span>>>> aptr = mytest.get_aptr() # obtain a shared-pointer to an instance of class A</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; background-color: transparent; font-style: normal;"><span>>>> aptr.f() # invoke method f() of class A via the pointer</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; background-color: transparent; font-style: normal;"><span>>>> b = mytest.B(aptr) # construct object of class B invoking constructor that accepts a shared-pointer to object of class A.</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; background-color:
 transparent; font-style: normal;"><span><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; background-color: transparent; font-style: normal;"><span>I am looking for the equivalent in R (or at least a close enough idiom).</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; background-color: transparent; font-style: normal;"><span><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; background-color: transparent; font-style: normal;"><span>Thanks</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; background-color: transparent; font-style: normal;"><span>suresh</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family:
 'Courier New', courier, monaco, monospace, sans-serif; background-color: transparent; font-style: normal;"><br></div><div><br></div>  <div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 12pt;"> <div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"> <div dir="ltr"> <font size="2" face="Arial"> <hr size="1">  <b><span style="font-weight:bold;">From:</span></b> Dirk Eddelbuettel <edd@debian.org><br> <b><span style="font-weight: bold;">To:</span></b> Suresh Easwar <seaswar@yahoo.com> <br><b><span style="font-weight: bold;">Cc:</span></b> "rcpp-devel@lists.r-forge.r-project.org" <rcpp-devel@lists.r-forge.r-project.org> <br> <b><span style="font-weight: bold;">Sent:</span></b> Wednesday, March 13, 2013 5:56 PM<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: [Rcpp-devel] Invoking class methods via XPtr<> objects in R<br> </font> </div> <br><br>On 13
 March 2013 at 10:15, Suresh Easwar wrote:<br>| How does one invoke class methods via XPtr<T> objects in R? I get this error: Error in aptr$f : object of type 'externalptr' is not subsettable:<br><br>I don't understand what you are trying to do below, and do not have time to<br>understand and debug it for you.<br><br>Your example is also complicated as you mix XPtr and Modules.  Maybe you will<br>have more success walking in smaller steps --- and a working example of XPtr<br>via Rcpp is in the RcppDE package where I use XPtr to pass external<br>(user-given) C interface functions around to the optimiser.<br><br>Dirk<br><br>|  <br>| cpp file:<br>| #include <Rcpp.h><br>| struct A {<br>| void f() {}<br>| };<br>| typedef Rcpp::XPtr<A> APtr;<br>| APtr getAPtr()<br>| {<br>| return APtr(new A);<br>| }<br>| struct B {<br>| B(APtr) { }<br>| };<br>| RCPP_MODULE(mytest)<br>| {<br>|                
 Rcpp::function("get_aptr", &getAPtr);<br>|                 Rcpp::class_<A>("A")<br>|                                 .constructor()<br>|                                 .method("f", &A::f)<br>|                 ;<br>|                 Rcpp::class_<B>("B")<br>|                                 .constructor<APtr>()<br>|                 ;<br>| }<br>|  <br>| R session:<br>| R version 2.15.3 (2013-03-01) -- "Security Blanket"<br>| Copyright (C) 2013 The R Foundation for Statistical Computing<br>| ISBN 3-900051-07-0<br>| Platform:
 x86_64-unknown-linux-gnu (64-bit)<br>| > library(mytest)<br>| Loading required package: Rcpp<br>| > a <- new(A)<br>| > aptr <- get_aptr()<br>| > b <- new(B, aptr)<br>| > a$f()<br>| > aptr$f()       <- not sure how to invoke f() via aptr<br>| Error in aptr$f : object of type 'externalptr' is not subsettable<br>| > <br>|  <br>| Thanks<br>|  <br>| Suresh<br>| _______________________________________________<br>| Rcpp-devel mailing list<br>| <a ymailto="mailto:Rcpp-devel@lists.r-forge.r-project.org" href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br><br>-- <br>Dirk Eddelbuettel | <a ymailto="mailto:edd@debian.org" href="mailto:edd@debian.org">edd@debian.org</a> | <a
 href="http://dirk.eddelbuettel.com/" target="_blank">http://dirk.eddelbuettel.com </a> <br><br><br> </div> </div>  </div></body></html>