C ++自定义迭代器和(* this)

时间:2017-03-22 20:08:36

标签: c++ iterator

我正在定义一个自定义迭代器

class row_iterator {
    // To iterate over nodes.
    friend class Hamiltonian;
    public:
        typedef row_iterator                 self_type;
        typedef int                          value_type;
        typedef std::forward_iterator_tag    iterator_category;
        row_iterator( const Bipartite & B ) : _B( other._B ) , _i( 0 ) {}
        row_iterator( const Bipartite & B , const int i ) : _B( other._B ) , _i( i ) {}
        row_iterator( const self_type & other ) : _B( other._B ) , _i( other._i ) {}
        self_type operator=( const self_type & other ) { _B = other._B; _i = other._i; return ( * this ); }
        self_type operator++()           { _i++; return ( * this ); } // PREFIX
        self_type operator++( int junk ) { self_type tmp = ( * this ); _i++; return tmp; } // POSTFIX  
        value_type & operator*() { return _i; }
        //value_type * operator->() { return & _i; }
        bool operator==( const self_type & rhs ) { return ( _i == rhs._i ) and ( _B == rhs._B ); }
        bool operator!=( const self_type & rhs ) { return ( _i != rhs._i ) or  ( _B != rhs._B ); }
        operator bool() { return _i < _B.num_rows(); }

        void test_func() {
            int i = ( * this );
            bool b = ( * this );
        }

    private:
        Bipartite & _B;
        int _i;
    };

注意函数test_func()。现在问题是......

如果我写

int i = ( * this );

我假设调用了int operator*()。另一方面,如果我写

bool b = ( * this );

我假设调用了operator bool()。是这种情况吗?

编辑:在这里,我添加了检查投票答案是否正确的测试。

#include <iostream>

class iter {
    public:
        typedef iter                         self_type;
        typedef int                          value_type;
        typedef std::forward_iterator_tag    iterator_category;
        iter( int imax ) : _i( 0 ) , _imax( imax ) {}
        value_type operator*() { return _i; }
        self_type operator++()           { _i++; return ( * this ); } // PREFIX
        self_type operator++( int junk ) { self_type tmp = ( * this ); ++( * this ); return tmp; } // POSTFIX  
        operator bool() { return _i < _imax; }
        void test() {
            bool b = ( * this );
            int q = ( * this );
            int i = ( * ( * this ) );
            std::cout << "b = " << b << " , q = " << q << " , i = " << i << std::endl;
        }
    private:
        int _i;
        int _imax;
};

int main( void ) {

    iter it( 10 );
    while ( ( bool ) it ) {
        it.test();
        it++;
    }

}

输出显示:

b = 1 , q = 1 , i = 0
b = 1 , q = 1 , i = 1
b = 1 , q = 1 , i = 2
b = 1 , q = 1 , i = 3
b = 1 , q = 1 , i = 4
b = 1 , q = 1 , i = 5
b = 1 , q = 1 , i = 6
b = 1 , q = 1 , i = 7
b = 1 , q = 1 , i = 8
b = 1 , q = 1 , i = 9

1 个答案:

答案 0 :(得分:3)

你的第二个假设是正确的。你的第一个不是。

您必须牢记row_iterator*的类型为*this,因此row_iterator的结果类型为operator bool(),由于您的存在bool b = *this可以在上下文中转换为bool,这是您的第二个作业(int i = (*this))中发生的事情。

在您的第一个作业(row_iterator)中,没有从intbool的合适转化。但是,转换为bool并将int转换为i也是可能的,因此在当前状态下,您的第一个作业将导致operator bool()成为this如果 "sonata-project/admin-bundle": "^2.3.0", "sonata-project/doctrine-orm-admin-bundle": "^2.3.4", "sonata-project/media-bundle": "2.3.x-dev", "sonata-project/core-bundle": "^2.3.0", "sonata-project/intl-bundle": "^2.3.0", "doctrine/doctrine-migrations-bundle": "dev-master", "symfony-cmf/block-bundle": "^1.3.1", "jackalope/jackalope-doctrine-dbal": "1.2.*", "sonata-project/doctrine-phpcr-admin-bundle": "^1.2", "doctrine/doctrine-cache-bundle": "1.0.*" 返回true,则返回1,否则返回0。

要解决此问题,您需要取消引用The following document types provided in valid_children are invalid: Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\StaticContent The class names provided could not be loaded 两次。

详细说明,fallback in this commit(这里发生的事情)最多包含三次转换:

  
      
  1. 零个或一个标准转换序列
  2.   
  3. 零或一个用户定义的转化
  4.   
  5. 零或一个标准转换序列
  6.   

标准转换序列包括:

  
      
  1. 零或一个左值变换
  2.   
  3. 零或一个数字促销或转化
  4.   
  5. 零或一个函数指针转换(c ++ 17及更高版本)
  6.   
  7. 零或一个资格调整
  8.   

由于标准转换序列不允许取消引用指针,因此在隐式转换期间解除引用的唯一方法是在用户定义的转换函数中完成。