Skip to content
Prev 6514 / 10988 Next

[Rcpp-devel] Problem exposing inherited member function of derived class to R through RCPP_MODULE

Hi All,

Just a comment on a side effect/issue with my previously posted solution to this problem, i.e.:
Like follows:

Class A {
	public:
		void fun2(){ };
		void fun1(){ fun2(); };
}

Class A_derived : public A {
	public:
		void fun2(){ };
		void fun1(){ A::fun1(); };
}

RCPP_MODULE(testing) {
      class_<A_derived>("A_derived")
      .method("fun1", &A_derived::fun1)
      ;
}

The issue here is that the call tree under A::fun1() will only call functions of the base class unless they are defined virtual, so any functions you re implement in the derived class are ignored. In the above example, this means A::fun2() is called from A::fun1() instead of A_derived::fun2(). A::fun2() must be declared as virtual for A_derived::fun2() to be called from A::fun1().

If for some reason you cannot make A::fun2() virtual (e.g. fun2 need to be a template), you can just create a wrapper function in the derived class and expose this to R with its name changed to the function being wrapped. E.g.

I am not sure how to overcome this problem if (for some reason) you cannot use a virtual function, e.g. fun2 is a template function.

Cheers,
Luke Domanski