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

C++ C++;自定义迭代器和(*this),c++,iterator,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_i

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

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;
    };
我假设调用了
int操作符*()
。另一方面,如果我写

bool b = ( * this );
我假设调用了
操作符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++;
    }

}

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

您必须记住,
this
的类型是
行迭代器*
,因此,
*this
的结果类型是
行迭代器
,由于存在
运算符bool()
可以在上下文中转换为bool,这是在第二次赋值中发生的情况(
bool b=*this

在您的第一个赋值(
int i=(*this)
)中,没有从
行迭代器
int
的合适转换。但是,可以将
bool
转换为
bool
,也可以将
bool
转换为
int
,因此在当前状态下,如果
运算符bool()
返回true,则第一次赋值将导致
i
为1,否则为0

为了解决这个问题,您需要取消引用
这个
两次

要详细说明,(这里正在发生的事情)最多包括三个转换:

  • 零或一个标准转换序列
  • 零或一个用户定义的转换
  • 零或一个标准转换序列
  • 标准转换顺序包括:

  • 零或一左值变换
  • 零或一个数字升级或转换
  • 零或一个函数指针转换(c++17及以上)
  • 零或一次资格调整

  • 由于标准转换序列不允许取消对指针的引用,因此在隐式转换过程中进行取消引用的唯一方法是在用户定义的转换函数中进行。您的第二个假设是正确的。你的第一个不是

    您必须记住,
    this
    的类型是
    行迭代器*
    ,因此,
    *this
    的结果类型是
    行迭代器
    ,由于存在
    运算符bool()
    可以在上下文中转换为bool,这是在第二次赋值中发生的情况(
    bool b=*this

    在您的第一个赋值(
    int i=(*this)
    )中,没有从
    行迭代器
    int
    的合适转换。但是,可以将
    bool
    转换为
    bool
    ,也可以将
    bool
    转换为
    int
    ,因此在当前状态下,如果
    运算符bool()
    返回true,则第一次赋值将导致
    i
    为1,否则为0

    为了解决这个问题,您需要取消引用
    这个
    两次

    要详细说明,(这里正在发生的事情)最多包括三个转换:

  • 零或一个标准转换序列
  • 零或一个用户定义的转换
  • 零或一个标准转换序列
  • 标准转换顺序包括:

  • 零或一左值变换
  • 零或一个数字升级或转换
  • 零或一个函数指针转换(c++17及以上)
  • 零或一次资格调整

  • 由于标准转换序列不允许取消对指针的引用,因此在隐式转换期间,唯一可以进行取消引用的方法是在用户定义的转换函数中进行。啊,你说得对
    *此
    是一个
    行迭代器&
    ,而
    **此
    是一个
    整型&
    。这很有意义。我已经测试过了,它就像你说的那样工作。我将添加测试。@Juan为了完整性起见,我已经添加了一个关于转换过程的简要说明。啊,你说得对
    *此
    是一个
    行迭代器&
    ,而
    **此
    是一个
    整型&
    。这很有意义。我已经测试过了,它就像你说的那样工作。我将添加测试。为了完整性起见,我已经添加了一个关于转换过程的简要说明。
    #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