自己的容器类的C ++迭代器和const_iterator问题 [英] C++ iterator and const_iterator problem for own container class

查看:299
本文介绍了自己的容器类的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()调用非const begin()。由于 myc 不是const,它无法知道你是否使用const begin()而不是非const 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迭代器包含一个转换运算符,它允许迭代器静默转换为 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

class const_iterator
{
public:
    typedef const_iterator self_type;

    const_iterator(iterator& ) {}
    inline const_iterator() { }
};

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

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