<div dir="auto">Excellent. Note that the clone method was required for my use case, but it may be superfluous for yours.<div dir="auto"><br></div><div dir="auto">Iñaki</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">El vie., 5 mar. 2021 17:15, Subhomoy Ghosh <<a href="mailto:subhomoy25@gmail.com">subhomoy25@gmail.com</a>> escribió:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks, Iñaki! Your package gave me enough hints to create the following attempt which seemed to have worked.<div><br></div><div>#include <RcppArmadillo.h></div><div><br>using namespace Rcpp;<br>using namespace arma;<br><br>// [[Rcpp::depends(RcppArmadillo)]]<br><br>class Distribution {<br><br>public:<br>  virtual Distribution* clone() {return (new Distribution(*this));}<br>  virtual ~Distribution() {}<br><br>};<br><br>template <typename T><br>class Uniform : public Distribution {<br><br>public:<br>  virtual Uniform<T>* clone() {return (new Uniform<T>(*this));}<br>  Uniform(const T &max,int i_) :<br>  max(max), i(i_) {}<br><br>  ~Uniform(){};<br><br>  T max;<br>  int i;<br>};<br><br><br><br>// [[Rcpp::export]]<br>XPtr<Distribution> getUniformParam( SEXP max,int i,int type) {<br>  // create pointer to an Uniform object and<br>  // wrap it as an external pointer for base class<br>  if(type==1) {<br>          return Rcpp::XPtr<Distribution> (new Uniform<double>(as<double>(max), i) );<br>  } else if(type==2) {<br>          return Rcpp::XPtr<Distribution> (new Uniform<int>(as<int>(max), i) );<br><br>   }<br>  // return the external pointer to the R side<br>  return Rcpp::XPtr<Distribution>(R_NilValue);<br>}<br><br><br><br>// [[Rcpp::export]]<br>double test2(double z, XPtr<Distribution> xp, int type) {<br><br>  Distribution* d=xp->clone();<br>  double tt=1;<br>  // convert the base pointer to derived class pointer<br>  if(type==1){ <br>       Uniform<double> *f = dynamic_cast<Uniform<double> *>(d);<br>       tt = z * f->max; <br>  } else if (type==2){<br>       Uniform<int> *f = dynamic_cast<Uniform<int> *>(d);<br>       tt =z * f->max;<br>    }<br>  <br> return tt;<br>}</div><div><br></div><div>// R side</div><div>xp<- getUniformParam(2.3,2,1)<br>test2(2,xp,1)<br><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Mar 4, 2021 at 2:58 PM Iñaki Ucar <<a href="mailto:iucar@fedoraproject.org" target="_blank" rel="noreferrer">iucar@fedoraproject.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Thu, 4 Mar 2021 at 20:31, Subhomoy Ghosh <<a href="mailto:subhomoy25@gmail.com" target="_blank" rel="noreferrer">subhomoy25@gmail.com</a>> wrote:<br>
><br>
> Thanks, for the solution. I tried what you suggested and it shows "no matching constructor for initialization of 'Rcpp::XPtr<Distribution>'". Below here is my attempt:<br>
><br>
> class Distribution {<br>
><br>
> public:<br>
>   template <typename T><br>
>   class Uniform2 {<br>
<br>
This is a nested class, which makes little sense in this context.<br>
Uniform2 should *inherit* from Distribution. Again, see my package as<br>
an example, which does exactly what you want.<br>
<br>
-- <br>
Iñaki Úcar<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr">Subhomoy</div></div>
</blockquote></div>