Hi,<div><br></div><div>Other than the approaches mentioned, I can also specialize an as<World>(SEXP) as<div><br></div><div><div> template <></div><div> World as<World>(SEXP x) {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Rcpp::XPtr<World> ptr(x);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>return World(ptr->greet());</div><div> }</div><div><br></div><div>However, this also gives segfault at runtime when addMsg is called. The final code, with alternative implementations commented out for experimentation is:</div>
<div><br></div><div><div><font color="#006600" face="'arial narrow', sans-serif">#include <RcppCommon.h></font></div><div><font color="#006600" face="'arial narrow', sans-serif">#include <string></font></div>
<div><font color="#006600" face="'arial narrow', sans-serif">class World {</font></div><div><font color="#006600" face="'arial narrow', sans-serif">public:</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> World() : msg("hello"){}</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> World(const std::string s) : msg(s){}</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> void set(std::string msg) { this->msg = msg; }</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> std::string greet() const { return msg; }</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> void addMsg(const World w2) {</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span>this->msg = this->msg + w2.greet();</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> }</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif">// World(SEXP x);</font></div><div><font color="#006600" face="'arial narrow', sans-serif">/* World(SEXPREC* &w2) {</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span>msg = ((World*)w2)->greet();</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> }</font></div><div><font color="#006600" face="'arial narrow', sans-serif">*/</font></div><div><font color="#006600" face="'arial narrow', sans-serif">private: </font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> std::string msg;</font></div><div><font color="#006600" face="'arial narrow', sans-serif">};</font></div><div><font color="#006600" face="'arial narrow', sans-serif">namespace Rcpp{</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><br></font></div><div><font color="#006600" face="'arial narrow', sans-serif">template <> SEXP wrap(const World& object);</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif">template <> World as<World>(SEXP x);</font></div><div><font color="#006600" face="'arial narrow', sans-serif">}</font></div><div><font color="#006600" face="'arial narrow', sans-serif">#include <Rcpp.h></font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><br></font></div><div><font color="#006600" face="'arial narrow', sans-serif"><br></font></div><div><font color="#006600" face="'arial narrow', sans-serif">namespace Rcpp {</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> template <></font></div><div><font color="#006600" face="'arial narrow', sans-serif"> SEXP wrap( const World& object ){</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> Language call( "new", Symbol( "World" ), object.greet() ) ;</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> return call.eval() ;</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> }</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> template <></font></div><div><font color="#006600" face="'arial narrow', sans-serif"> World as<World>(SEXP x) {</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span>Rcpp::XPtr<World> ptr(x);</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span>return World(ptr->greet());</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> }</font></div><div><font color="#006600" face="'arial narrow', sans-serif">}</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><br>
</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><br></font></div><div><font color="#006600" face="'arial narrow', sans-serif">/*World::World(SEXP x) {</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span>Rcpp::XPtr<World> ptr(x);</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span>msg = ptr->greet();</font></div><div><font color="#006600" face="'arial narrow', sans-serif">}*/</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><br></font></div><div><div><font color="#006600" face="'arial narrow', sans-serif">//...</font></div></div></div><div><div><font color="#006600" face="'arial narrow', sans-serif"><br>
</font></div><div><font color="#006600" face="'arial narrow', sans-serif">RCPP_MODULE(yada){</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span>using namespace Rcpp ;</font></div>
</div><div><div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span>class_<World>( "World" )</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span> // expose the default constructor</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span> .constructor()</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> .constructor<std::string>()</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">        </span> <span class="Apple-tab-span" style="white-space:pre">        </span>.method( "greet", &World::greet , "get the message" )</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">                </span>.method( "set", &World::set , "set the message" )</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span class="Apple-tab-span" style="white-space:pre">                </span>.method( "addMsg", &World::addMsg , "add the message from another object" );</font></div>
</div><div><font color="#006600" face="'arial narrow', sans-serif">}</font></div><div><br></div><div>Then, running with R -d gdb</div><div><div>> a <- new(World,"hello ")</div><div>> b <- new(World,"worlds")</div>
<div>> a$greet( )</div><div>[1] "hello "</div><div>> a$addMsg( b)</div><div><br></div><div>Program received signal EXC_BAD_ACCESS, Could not access memory.</div><div>Reason: KERN_INVALID_ADDRESS at address: 0x0000000011000078</div>
<div>0x00000001042032df in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string ()</div><div>(gdb) bt</div><div>#0 0x00000001042032df in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string ()</div>
<div>#1 0x0000000104358b18 in Rcpp::as<World> ()</div><div>#2 0x000000010435d564 in Rcpp::CppMethod1<World, void, World>::operator() ()</div><div>#3 0x000000010435eadb in Rcpp::class_<World>::invoke_void ()</div>
<div>#4 0x0000000104164670 in CppMethod__invoke_void ()</div><div>#5 0x00000001000928e9 in do_External ()</div><div>#6 0x00000001000c4af7 in Rf_eval ()</div><div>#7 0x00000001000c6d99 in do_begin ()</div><div>#8 0x00000001000c48af in Rf_eval ()</div>
<div>#9 0x00000001000c7cf3 in Rf_applyClosure ()</div><div>#10 0x00000001000c4778 in Rf_eval ()</div><div>#11 0x00000001000fed75 in Rf_ReplIteration ()</div><div>#12 0x00000001000ff021 in R_ReplConsole ()</div><div>#13 0x00000001000ff590 in run_Rmainloop ()</div>
<div>#14 0x0000000100000e8b in main ()</div><div>(gdb) </div></div><div><br></div><div>Thanks!</div></div><br><div class="gmail_quote">On Wed, Dec 21, 2011 at 7:41 PM, Darren Cook <span dir="ltr"><<a href="mailto:darren@dcook.org">darren@dcook.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Yasir,<br>
Could you post your complete (minimal) code to the list. Then someone<br>
might be curious enough to try reproducing the problem.<br>
<span class="HOEnZb"><font color="#888888"><br>
Darren<br>
</font></span><div class="im"><br>
<br>
On 2011-12-22 08:29, Yasir Suhail wrote:<br>
> Thanks Darren, Douglas, Richard, and Dirk!<br>
><br>
> I can compile with the following definitions:<br>
><br>
> World(SEXP x) {<br>
> Rcpp::XPtr<World> ptr(x);<br>
> msg = ptr->msg;<br>
> }<br>
<br>
</div>> ...<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
--<br>
Darren Cook, Software Researcher/Developer<br>
<br>
<a href="http://dcook.org/work/" target="_blank">http://dcook.org/work/</a> (About me and my work)<br>
<a href="http://dcook.org/blogs.html" target="_blank">http://dcook.org/blogs.html</a> (My blogs and articles)<br>
</div></div></blockquote></div><br></div>