[Rcpp-devel] Wrapping a C struct in C++ for constructor/destructor

Douglas Bates bates at stat.wisc.edu
Fri Dec 14 17:10:25 CET 2012


This is more a C++ question than an Rcpp question although, of course, the
application will be through Rcpp.

I want to use C functions defined in the CHOLMOD package using Rcpp.  The
arguments to these functions and the returned values are usually pointers
to C structs. There are several such structs defined in CHOLMOD and it gets
tedious to use code that allocates storage for the struct, populates the
struct, uses it and then frees it.  (If you look in the Matrix package
sources you will see several inelegant utility functions and macros to
perform these operations).

For an Rcpp/C++ approach I would prefer to use constructors and destructors
for the memory allocation and freeing.  Is the recommended approach to
create a C++ class or struct that extends the C struct or to create a C++
class with the C struct as a member of the class?

I should be able to create the code for myself if I just know where to look
for examples.  I tried StackOverflow but I was unable to narrow down my
search.  Pointers to sections of "C++ Annotations" or to StackOverflow
questions would be appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121214/2d59d58c/attachment.html>


More information about the Rcpp-devel mailing list