<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 20px; font-family: Calibri, sans-serif; "><div><div><div>Hi all,</div><div>I've just started using Rcpp. This is a brief example using the inline package to show how to define a C++ class and use it in R.</div><div>The RCPP_MODULE section translates the C++ class to methods accesible from R.</div><div><span class="Apple-style-span" style="font-family: Monaco; font-size: 14px; color: rgb(75, 144, 100); "><br></span></div><div><span class="Apple-style-span" style="font-family: Monaco; font-size: 14px; color: rgb(75, 144, 100); "># Remember, this is an R file </span></div><div>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># Author: mcasl</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064">###############################################################################</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">require(<span style="color: #5655ba">"inline"</span>)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">require(<span style="color: #5655ba">"Rcpp"</span>)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">test_code <- <span style="color: #5655ba">'</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">        </span>class Con {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                </span>public:</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>Con (int f_, double w_) : from(f_), weight(w_) {}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>int <span class="Apple-tab-span" style="white-space:pre">        </span>from;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>double weight;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>int<span class="Apple-tab-span" style="white-space:pre">                </span>getFrom<span class="Apple-tab-span" style="white-space:pre">                </span>() <span class="Apple-tab-span" style="white-space:pre">                </span>{return(from);};</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>double <span class="Apple-tab-span" style="white-space:pre">        </span>getWeight<span class="Apple-tab-span" style="white-space:pre">        </span>() <span class="Apple-tab-span" style="white-space:pre">                        </span>{return(weight);};</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>void <span class="Apple-tab-span" style="white-space:pre">        </span>setFrom<span class="Apple-tab-span" style="white-space:pre">                </span>(int f) <span class="Apple-tab-span" style="white-space:pre">        </span>{from = f;};</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>void <span class="Apple-tab-span" style="white-space:pre">        </span>setWeight<span class="Apple-tab-span" style="white-space:pre">        </span>(double w) <span class="Apple-tab-span" style="white-space:pre">        </span>{weight = w;};</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                </span>};</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">        </span>RCPP_MODULE(AMORE_module) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">        </span>class_<Con>( "Con" )</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>.constructor<int, double> ()</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>.field( "from", &Con::from )</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>.field( "weight", &Con::weight )</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>.method( "getFrom", &Con::getFrom )</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>.method( "setFrom", &Con::setFrom )</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>.method( "getWeight", &Con::getWeight )</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">                        </span>.method( "setWeight", &Con::setWeight )</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">        </span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba"><span class="Apple-tab-span" style="white-space:pre">        </span>};</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #5655ba">'</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">fx <- cxxfunction( signature(), <span style="color: #5655ba">""</span> , include = test_code, plugin = <span style="color: #5655ba">"Rcpp"</span> )</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">AMOREmod <- Module(<span style="color: #5655ba">"AMORE_module"</span>, getDynLib(fx))</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">gCon <- AMOREmod<span style="color: #4f7272">$</span>Con</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># Loading required package: inline</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># Loading required package: Rcpp</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">gCon </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># C++ class 'Con' <0x10046e500></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># Constructors:</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># Con(int, double)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># Fields: </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># int from</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># double weight</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># Methods: </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># int getFrom() </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># double getWeight() </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># void setFrom(int) </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># void setWeight(double) </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">mycon <- gCon<span style="color: #4f7272">$</span>new(as.integer(<span style="color: #15148f">5</span>), <span style="color: #15148f">4.3</span>)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># </p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">mycon</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># C++ object <0x100466b00> of class 'Con' <0x10046e500></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">mycon<span style="color: #4f7272">$</span>getFrom()</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">mycon<span style="color: #4f7272">$</span>setFrom(<span style="color: #15148f">2</span>)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">mycon<span style="color: #4f7272">$</span>getFrom()</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># [1] 5</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #4b9064"># [1] 2</p></div><div><br></div></div></div></body></html>