从“foo”转换为到“const foo” - C++
我有一个类似的函数(请不要关心通过引用返回临时值。这只是解释问题的示例),
const foo<const int>& get_const()
{
foo<int> f;
return f;
}
这显然无法编译。我正在寻找一种方法来确保调用者不会更改 foo
的 T
。我怎样才能确保这一点?
我已经看到 boost::shared_ptr
的类似行为。 shared_ptr
可转换为 const shared_ptr
。我不明白它是如何做到这一点的。
任何帮助都会很棒。
I have a function like (please don't care about returning temporary by reference. This is just an example to explain the problem),
const foo<const int>& get_const()
{
foo<int> f;
return f;
}
This obviously won't compile. I am looking for a way to ensure callers won't change the T
of foo
. How can I ensure that?
I have seen the similar behavior for boost::shared_ptr
. shared_ptr<T>
is convertible to const shared_ptr<const T>
. I couldn't figure out how it is doing this.
Any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编译器将
foo
和foo
视为两个完全不同且不相关的类型,因此foo
类需要支持这一点与任何其他转换一样显式地进行。如果您可以控制 foo 类,则需要提供复制构造函数或隐式转换运算符(或两者)。The compiler sees
foo<T>
andfoo<const T>
as two completely different and unrelated types, so thefoo
class needs to support this explicitly just as with any other conversion. If you have control over thefoo
class, you need to provide a copy constructor or an implicit conversion operator (or both).假设 Foo 的定义如下:
然后,为了确保 Foo 的客户端不会修改 m_value (我假设这就是“我正在寻找一种方法来确保调用者不会更改foo"),您需要对 Foo 对象而不是其模板参数进行 const 限定,即
因此,您的 get_foo 函数应返回
const Foo&
,而不是const Foo< ;const T>&
。这是一个完整的、可编译的示例:
Assuming that Foo is defined something like this:
Then, in order to ensure that clients of Foo do not modify m_value (I assume that this is what is meant by "I am looking for a way to ensure callers won't change the T of foo"), you need to const-qualify the Foo object rather than its template parameter, i.e.
Therefore, your get_foo function should return a
const Foo<T>&
, not aconst Foo<const T>&
.Here's a complete, compilable example:
如果我没记错的话,boost::shared_ptr 实现有一个非显式构造函数,它接受 const T& 引用作为参数,然后使用
在 RHS 指针上使用 const_cast
来删除const
,从而允许它们之间的隐式转换。像这样的事情:
这是你要找的吗?
If I'm not mistaken, the
boost::shared_ptr
implementation has a non-explicit constructor that takes aconst T&
reference as an argument and then uses aconst_cast
on the RHS's pointer to remove theconst
, allowing implicit conversions between them.Something like this:
Is that what you're looking for?
首先,您通过引用返回本地对象......这不好。
foo 和 foo 是两种不同的类型,因此您必须编写代码(转换构造函数)来显式转换它们。
为了得到你想要的,考虑一下:
如果你正确地完成了封装并且只访问带有 const getter 和非常量 setter 的成员,这对你来说应该足够了。请记住,如果人们真的想更改您的对象,他们总是可以 const_cast。常量正确性只是为了发现无意的错误。
First of all, you're returning a local object by reference...that's not good.
foo and foo are two different types so you'll have to write code (conversion constructors) to explicitly convert them.
To get what you wanted, consider this:
If you done your encapsulation correctly and only access members with const getter and non-const setters, this should be good enough for you. Remember if people really want to change your object, they can always const_cast. Const-correctness is only to catch unintentional mistakes.