I am trying to wrap a C structure in a C++ class to take advantage of memory management and such. I have mad the structure a private member and provided a public function to provide access. The return type is constant, since all functions that take the object as an argument have const
in their signature.
#include <gsl/gsl_rng.h>
class GSLRand {
gsl_rng* r_; // see links below
public:
GSLRand() {
gsl_rng_env_setup();
r_ = gsl_rng_alloc(gsl_rng_default);
}
~GSLRand() {
gsl_rng_free(r_);
}
const gsl_rng* rng() {
return r_;
}
};
That all compiles nicely. The problem occurs when I get clever and try to add a copy constructor. Introducing it into the class like...
public:
....
GSLRand(const GSLRand& R) {
r_ = gsl_rng_alloc(gsl_rng_taus);
gsl_rng_memcpy(r_, R.rng());
}
....
I get the following compiler error:
GSLRand.h: In copy constructor ‘GSLRand::GSLRand(const GSLRand&)’: GSLRand.h:35: error: passing ‘const GSLRand’ as ‘this’ argument of ‘gsl_rng* GSLRand::rng()’ discards qualifiers
I'm using g++ on a Mac. I have tried the different variants and still can't figure out how I'm confusing the compiler (or myself!). Interestingly, I get the identical error when I remove the const
specifier from rng()
.
Any ideas?
For documentation of the functions that are used: random number generation, the sections on "environment variables" and "copying generators."