可以投射一个< Key,Value>到一对< const Key,Value>? [英] Is it possible to cast a pair<Key, Value> to a pair<const Key, Value>?

查看:166
本文介绍了可以投射一个< Key,Value>到一对< const Key,Value>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个智能迭代器模拟映射const_iterator,它需要在内部构建返回类型。显然,我想在我的迭代器类中存储一个对< Key,Value> (因为我需要修改它),但同时我想解除引用函数呈现对< const Key,Value> (实际上它将是 const对< const Key,Value& code>和 const pair< const Key,Value> * )。我到目前为止唯一的解决方案是动态分配一个新的对,每次改变我的迭代器类指向的变化值。不必说,这不是一个好的解决方案。



我也试过了 * const_cast< const pair< const Key,Value& >(& value)其中 value 声明为 pair< Key,Value>



任何帮助将非常感激(因为它不能做到的知识)。



EDIT



对于好奇我最后存储了一个 pair< const Key,Value& p 在我的迭代器类。为了改变对,我基于底层迭代器( map< Key,Value> :: const_iterator it )分别修改两个元素, const_cast

$

  * const_cast< Key *> 

;(& p.first)= it-> first;
p.second = it-> second;

不是一个我非常高兴的解决方案,但它完成了工作,

解决方案

您可以转换 >< const Key,Value> < / p>

但是,仔细阅读这个问题,你实际上是询问是否给予对< Key,Value> 可以为 pair< const Key,Value> 创建指针 >

答案是否定的,唯一的情况是一个类型的引用或指针可以引用不同类型的对象,如果对象类型从引用的类型继承。 p>

一种可能性是返回一对引用, pair< const Key& Value;&> 您希望参考的对。


So I have a smart iterator that emulates a map const_iterator, and it needs to build the return type internally. Obviously, I'd like to store a pair<Key, Value> in my iterator class (since I need to modify it), but at the same time I'd like the dereference functions to present a pair<const Key, Value> (actually it would be a const pair<const Key, Value>& and const pair<const Key, Value>* respectively). The only solution I've come up with so far is to dynamically allocate a new pair every time change the value that my iterator class points to changes. Needless to say, this is not a good solution.

I've also tried *const_cast<const pair<const Key, Value> >(&value) where value is declared as pair<Key, Value>.

Any help would be greatly appreciated (as would the knowledge that it can't be done).

EDIT

For the curious: I ended up storing a pair<const Key, Value> p in my iterator class. In order to change the pair I alter the two elements separately based on the underlying iterator (map<Key, Value>::const_iterator it), const_casting the key so that it could be altered, like this:

*const_cast<Key*>(&p.first) = it->first;
p.second = it->second;

Not a solution I'm terribly happy with, but it gets the job done, and the dereference methods are happy because I'm storing something of the correct type, which they can refer to.

解决方案

You can convert a value of type pair<Key,Value> to pair<const Key,Value>.

However, reading the question carefully, you're actually asking if, given a pair<Key,Value> you can create a pointer or reference to pair<const Key,Value> referring to the same object.

The answer is no - the only situation where a reference or pointer to one type can refer to an object of a different type is if the object type inherits from the referenced type.

One possibility is to return a pair of references, pair<const Key&, Value&>, created from the pair you wish to reference.

这篇关于可以投射一个&lt; Key,Value&gt;到一对&lt; const Key,Value&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆