复制构造函数的 const 正确性问题?

发布于 2024-09-25 07:27:08 字数 1173 浏览 2 评论 0原文

我正在尝试将 C 结构包装在 C++ 类中以利用内存管理等。我已将该结构设为私有成员,并提供了公共函数来提供访问权限。返回类型是常量,因为所有将对象作为参数的函数的签名中都有 const

#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_;
    }   
};

这一切编译得很好。当我变得聪明并尝试添加复制构造函数时,问题就出现了。将其引入类中,例如...

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

我收到以下编译器错误:

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

我在 Mac 上使用 g++。我已经尝试了不同的变体,但仍然无法弄清楚我是如何混淆编译器(或我自己!)的。有趣的是,当我从 rng() 中删除 const 说明符时,我得到了相同的错误。

有什么想法吗?

有关所使用函数的文档: 随机数生成,“环境”部分变量”和“复制生成器”。

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."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

や莫失莫忘 2024-10-02 07:27:08

rng() 设为 const 函数:const gsl_rng* rng() const {

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

時窥 2024-10-02 07:27:08

将此函数更改为:

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

Change this function to:

const gsl_rng* rng() const {
    return r_;
}   
慕巷 2024-10-02 07:27:08

两个问题。首先,您通过 const 对象引用调用非 const 成员函数。不能那样做。您可以将 GSLRand::rnd() 设为 const 成员函数:

const gsl_rng* rng() const {

...但是您有第二个问题:gsl_rng() 返回一个 const gsl_rng*,但您试图将其分配给非 const 成员变量。也不能这么做。

岔路口。您要么始终通过 r_ 指针调用 const 成员函数,要么有时通过它调用非 const 成员函数。

如果您始终调用 const 成员函数,则使成员变量指向 const gsl_rng

const class gsl_rng* r_;    // see links below

否则,使您的 rng() 函数返回非 const 指针,同时保持方法本身 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_;
    }   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文