<i id='e3lvN'><tr id='e3lvN'><dt id='e3lvN'><q id='e3lvN'><span id='e3lvN'><b id='e3lvN'><form id='e3lvN'><ins id='e3lvN'></ins><ul id='e3lvN'></ul><sub id='e3lvN'></sub></form><legend id='e3lvN'></legend><bdo id='e3lvN'><pre id='e3lvN'><center id='e3lvN'></center></pre></bdo></b><th id='e3lvN'></th></span></q></dt></tr></i><div id='e3lvN'><tfoot id='e3lvN'></tfoot><dl id='e3lvN'><fieldset id='e3lvN'></fieldset></dl></div>

      <tfoot id='e3lvN'></tfoot>

        <bdo id='e3lvN'></bdo><ul id='e3lvN'></ul>

      <small id='e3lvN'></small><noframes id='e3lvN'>

    1. <legend id='e3lvN'><style id='e3lvN'><dir id='e3lvN'><q id='e3lvN'></q></dir></style></legend>

      自己的容器类的 C++ 迭代器和 const_iterator 问题

      C++ iterator and const_iterator problem for own container class(自己的容器类的 C++ 迭代器和 const_iterator 问题)
    2. <i id='OaA0N'><tr id='OaA0N'><dt id='OaA0N'><q id='OaA0N'><span id='OaA0N'><b id='OaA0N'><form id='OaA0N'><ins id='OaA0N'></ins><ul id='OaA0N'></ul><sub id='OaA0N'></sub></form><legend id='OaA0N'></legend><bdo id='OaA0N'><pre id='OaA0N'><center id='OaA0N'></center></pre></bdo></b><th id='OaA0N'></th></span></q></dt></tr></i><div id='OaA0N'><tfoot id='OaA0N'></tfoot><dl id='OaA0N'><fieldset id='OaA0N'></fieldset></dl></div>

          <tfoot id='OaA0N'></tfoot>
              <bdo id='OaA0N'></bdo><ul id='OaA0N'></ul>
            • <small id='OaA0N'></small><noframes id='OaA0N'>

            • <legend id='OaA0N'><style id='OaA0N'><dir id='OaA0N'><q id='OaA0N'></q></dir></style></legend>
                <tbody id='OaA0N'></tbody>

                本文介绍了自己的容器类的 C++ 迭代器和 const_iterator 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在编写一个自己的容器类,但遇到了一个我无法理解的问题.这是显示问题的基本示例.

                I'm writing an own container class and have run into a problem I can't get my head around. Here's the bare-bone sample that shows the problem.

                它由一个容器类和两个测试类组成:一个使用 std:vector 的测试类可以很好地编译,第二个测试类尝试以完全相同的方式使用我自己的容器类,但编译失败.

                It consists of a container class and two test classes: one test class using a std:vector which compiles nicely and the second test class which tries to use my own container class in exact the same way but fails miserably to compile.

                #include <vector>
                #include <algorithm>
                #include <iterator>
                
                using namespace std;
                
                template <typename T>
                class MyContainer
                {
                public:
                  
                  class iterator
                  {
                  public:
                    typedef iterator self_type;
                    inline iterator() { }
                  };
                
                  class const_iterator
                  {
                  public:
                    typedef const_iterator self_type;
                    inline const_iterator() { }
                  };
                  
                  iterator begin() {
                    return iterator();
                  }
                
                  const_iterator begin() const {
                    return const_iterator();
                  }
                };
                
                // This one compiles ok, using std::vector
                class TestClassVector
                {
                public:
                  void test() {
                    vector<int>::const_iterator I=myc.begin();
                  }
                
                private:
                  vector<int> myc;
                };
                
                // this one fails to compile. Why?
                class TestClassMyContainer
                {
                public:
                  void test(){
                    MyContainer<int>::const_iterator I=myc.begin();
                  }
                
                private:
                  MyContainer<int> myc;
                };
                
                
                int main(int argc, char ** argv)
                {
                  return 0;
                }
                

                gcc 告诉我:

                test2.C:在成员函数‘void TestClassMyContainer::test()’中:

                test2.C: In member function ‘void TestClassMyContainer::test()’:

                test2.C:51: 错误:请求从MyContainer::iterator"转换为非标量类型MyContainer::const_iterator"

                test2.C:51: error: conversion from ‘MyContainer::iterator’ to non-scalar type ‘MyContainer::const_iterator’ requested

                我不确定编译器在哪里以及为什么要将迭代器转换为我自己的类的 const_iterator 而不是 STL 向量类.我做错了什么?

                I'm not sure where and why the compiler wants to convert an iterator to a const_iterator for my own class but not for the STL vector class. What am I doing wrong?

                推荐答案

                当你调用 begin() 时,编译器默认会创建一个对非常量 begin() 的调用代码>.由于 myc 不是 const,它无法知道您的意思是使用 const begin() 而不是非常量 begin().

                When you call begin() the compiler by default creates a call to the non-const begin(). Since myc isn't const, it has no way of knowing you mean to use the const begin() rather than the non-const begin().

                STL 迭代器包含一个强制转换运算符,它允许将 iterator 静默转换为 const_iterator.如果你想让它工作,你需要像这样添加一个:

                The STL iterator contains a cast operator which allows an iterator to be silently converted to a const_iterator. If you want this to work you need to add one as well like so:

                class iterator
                {
                public:
                    typedef iterator self_type;
                    inline iterator() { }
                
                    operator const_iterator() { return const_iterator(); }
                };
                

                或允许 const_iterator 像这样从 iterator 构造:

                or allow const_iterator to be constructed from an iterator like so:

                class const_iterator
                {
                public:
                    typedef const_iterator self_type;
                
                    const_iterator(iterator& ) {}
                    inline const_iterator() { }
                };
                

                这篇关于自己的容器类的 C++ 迭代器和 const_iterator 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                STL BigInt class implementation(STL BigInt 类实现)
                Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

                    1. <small id='ES0hT'></small><noframes id='ES0hT'>

                      <tfoot id='ES0hT'></tfoot>

                      1. <legend id='ES0hT'><style id='ES0hT'><dir id='ES0hT'><q id='ES0hT'></q></dir></style></legend>
                        • <bdo id='ES0hT'></bdo><ul id='ES0hT'></ul>
                          <i id='ES0hT'><tr id='ES0hT'><dt id='ES0hT'><q id='ES0hT'><span id='ES0hT'><b id='ES0hT'><form id='ES0hT'><ins id='ES0hT'></ins><ul id='ES0hT'></ul><sub id='ES0hT'></sub></form><legend id='ES0hT'></legend><bdo id='ES0hT'><pre id='ES0hT'><center id='ES0hT'></center></pre></bdo></b><th id='ES0hT'></th></span></q></dt></tr></i><div id='ES0hT'><tfoot id='ES0hT'></tfoot><dl id='ES0hT'><fieldset id='ES0hT'></fieldset></dl></div>
                            <tbody id='ES0hT'></tbody>