c++ - const correctness problem with copy constructor? -


i trying wrap c structure in c++ class take advantage of memory management , such. have mad structure private member , provided public function provide access. return type constant, since functions take object argument have const in 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 compiles nicely. problem occurs when clever , try add copy constructor. introducing class like...

public: ....     gslrand(const gslrand& r) {         r_ = gsl_rng_alloc(gsl_rng_taus);         gsl_rng_memcpy(r_, r.rng()); } .... 

i following compiler error:

 gslrand.h: in copy constructor ‘gslrand::gslrand(const gslrand&)’: gslrand.h:35: error: passing ‘const gslrand’ ‘this’ argument of ‘gsl_rng* gslrand::rng()’ discards qualifiers 

i'm using g++ on mac. have tried different variants , still can't figure out how i'm confusing compiler (or myself!). interestingly, identical error when remove const specifier rng().

any ideas?

for documentation of functions used: random number generation, sections on "environment variables" , "copying generators."

make rng() const function: const gsl_rng* rng() const {.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -