复制构造函数的 const 正确性问题?
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
rng()
设为 const 函数:const gsl_rng* rng() const {
。Make
rng()
const function:const gsl_rng* rng() const {
.将此函数更改为:
Change this function to:
两个问题。首先,您通过 const 对象引用调用非 const 成员函数。不能那样做。您可以将
GSLRand::rnd()
设为const
成员函数:const gsl_rng* rng() const {
...但是您有第二个问题:gsl_rng() 返回一个 const gsl_rng*,但您试图将其分配给非 const 成员变量。也不能这么做。
岔路口。您要么始终通过 r_ 指针调用 const 成员函数,要么有时通过它调用非 const 成员函数。
如果您始终调用
const
成员函数,则使成员变量指向const gsl_rng
:否则,使您的
rng()
函数返回非 const 指针,同时保持方法本身 const:Two problems. First, you are calling a non-
const
member function through aconst
object reference. Can't do that. You can makeGSLRand::rnd()
aconst
member function:const gsl_rng* rng() const {
...but then you have a second problem:
gsl_rng()
returns aconst 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 ther_
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 aconst gsl_rng
:Otherwise, make your
rng()
function return a non-const
pointer, while keeping the method itselfconst
: