Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++,如何从迭代器返回对成员变量的引用_C++_Vector_Reference_Iterator_Set - Fatal编程技术网

C++ C++,如何从迭代器返回对成员变量的引用

C++ C++,如何从迭代器返回对成员变量的引用,c++,vector,reference,iterator,set,C++,Vector,Reference,Iterator,Set,下面是从std::vector派生的类B class B: public std::vector <unsigned int> { public: B() : std::vector <unsigned int> ( 0 ) {} }; 谢谢你的帮助 编辑问题: 使用const_cast的可能解决方案: B &il2 = const_cast <B&> ( i_alist->getB() ); B&il

下面是从std::vector派生的类B

class B: public std::vector <unsigned int>
{
    public:
            B() : std::vector <unsigned int> ( 0 ) {}
};
谢谢你的帮助

编辑问题

使用const_cast的可能解决方案:

B &il2 = const_cast <B&> ( i_alist->getB() );
B&il2=const_cast(i_-alist->getB());

这与
std::vector
关系不大,与
std::set
有关。将对象存储在
std::set
中。
std::set
的元素不能从外部修改。一旦将元素插入到集合中,就无法对其进行更改

因此,
std::set::iterator
可能(也将)计算为常量对象,即使它不是
const_迭代器
(语言规范实际上允许
std::set::iterator
std::set::const_iterator
引用相同的类型,但并不要求它)。例如,在您的示例中,
*I_list
是类型为
常量a
的常量限定对象。编译器将调用
getB
const
版本,该版本返回
const B&
。显然,您希望突破这种常量,希望调用非常量版本的
getB

除非您决定使用某种hack(
const_cast
等)从set元素中删除常量,从而使编译器调用非常量版本的
getB

基于
const\u cast
的黑客解决方案

B &il = const_cast<A &>(*i_alist).getB();

这与
std::vector
关系不大,与
std::set
关系密切。将对象存储在
std::set
中。
std::set
的元素不能从外部修改。一旦将元素插入到集合中,就无法对其进行更改

因此,
std::set::iterator
可能(也将)计算为常量对象,即使它不是
const_迭代器
(语言规范实际上允许
std::set::iterator
std::set::const_iterator
引用相同的类型,但并不要求它)。例如,在您的示例中,
*I_list
是类型为
常量a
的常量限定对象。编译器将调用
getB
const
版本,该版本返回
const B&
。显然,您希望突破这种常量,希望调用非常量版本的
getB

除非您决定使用某种hack(
const_cast
等)从set元素中删除常量,从而使编译器调用非常量版本的
getB

基于
const\u cast
的黑客解决方案

B &il = const_cast<A &>(*i_alist).getB();

您的示例中有一些看起来很奇怪的地方:

  • 永远不要从
    std::vector
    派生。它不是为那种用途而设计的。通常没有理由这么做,除了懒惰,也就是避免打字

  • A::operator()
    是干什么的?看起来是某种奇怪的方式为您正在使用的
    std::set
    提供了一个比较器。通常,您不会仅仅因为将类填充到某个
    std::set
    中而更改该类。如果你真的想比较你的A(一般来说,不仅仅是集合),那么就写一个合适的free
    bool操作符你的例子有一些奇怪的地方:

  • 永远不要从
    std::vector
    派生。它不是为那种用途而设计的。通常没有理由这么做,除了懒惰,也就是避免打字

  • A::operator()
    是干什么的?看起来是某种奇怪的方式为您正在使用的
    std::set
    提供了一个比较器。通常,您不会仅仅因为将类填充到某个
    std::set
    中而更改该类。如果你真的想比较你的A(通常不只是为集合),然后亲自编写一个适当的免费<代码> BOOL操作器,我认为这是C++标准的一个错误,它强迫在<代码> set < /Cux>键上运行常态。我经常在一个集合中有复杂的对象,其内容可以在不影响排序的情况下更改。这不是C++11引入的更改吗?@AndreyT:有没有办法允许存储在std::set中的对象可以从外部修改?例如,使用const_cast?@Mark Ransom:key本身的constness是有意义的,但是修改对象的非key部分的需要确实经常出现。是的,我同意,集合元素应该是可修改的。@MarkRansom,但是
    std::set
    如何保证您的更改不会影响排序?@aleguna:不会(可能是调试实现中的一些断言)。它只是简单地说“知道你在做什么,并冒着风险去做”。我个人认为,C++标准的错误是强迫在代码< > SET/COD>键上的一致性。我经常在一个集合中有复杂的对象,其内容可以在不影响排序的情况下更改。这不是C++11引入的更改吗?@AndreyT:有没有办法允许存储在std::set中的对象可以从外部修改?例如,使用const_cast?@Mark Ransom:key本身的constness是有意义的,但是修改对象的非key部分的需要确实经常出现。是的,我同意,集合元素应该是可修改的。@MarkRansom,但是
    std::set
    如何保证您的更改不会影响排序?@aleguna:不会(可能是调试实现中的一些断言)。它会简单地说“知道你在做什么,并自担风险”。
    B &il2 = const_cast <B&> ( i_alist->getB() );
    
    B &il = const_cast<A &>(*i_alist).getB();
    
    B &il = const_cast<B &>((*i_alist).getB());
    B &il2 = const_cast<B &>(i_alist->getB());