Note that there are some explanatory texts on larger screens.

plurals
  1. POconst correctness problem with copy constructor?
    primarykey
    data
    text
    <p>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 <code>const</code> in their signature.</p> <pre><code>#include &lt;gsl/gsl_rng.h&gt; 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_; } }; </code></pre> <p>That all compiles nicely. The problem occurs when I get clever and try to add a copy constructor. Introducing it into the class like...</p> <pre><code>public: .... GSLRand(const GSLRand&amp; R) { r_ = gsl_rng_alloc(gsl_rng_taus); gsl_rng_memcpy(r_, R.rng()); } .... </code></pre> <p>I get the following compiler error:</p> <pre> 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 </pre> <p>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 <code>const</code> specifier from <code>rng()</code>.</p> <p>Any ideas?</p> <p>For documentation of the functions that are used: <a href="http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Generation.html" rel="nofollow">random number generation</a>, the sections on "environment variables" and "copying generators."</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload