开发者

const correctness problem with copy constructor?

开发者 https://www.devze.com 2023-01-17 23:08 出处:网络
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

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 fig开发者_高级运维ure 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."


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


Change this function to:

const gsl_rng* rng() const {
    return r_;
}   


Two problems. First, you are calling a non-const member function through a const object reference. Can't do that. You can make GSLRand::rnd() a const member function:

const gsl_rng* rng() const {

...but then you have a second problem: gsl_rng() returns a const gsl_rng*, but you're trying to assign this to a non-const member variable. Can't do that, either.

Fork in the road. Either you always call const member functions on through the r_ pointer, or you sometimes call non-const member functions through it.

If you always call const member functions, then make the member variable point to a const gsl_rng:

const class gsl_rng* r_;    // see links below

Otherwise, make your rng() function return a non-const pointer, while keeping the method itself const:

gsl_rng* rng() const {
        return r_;
    }   
0

精彩评论

暂无评论...
验证码 换一张
取 消