k*******3 发帖数: 1909 | 1 下面程序copy来自C++ essential 4.7节,
Triangular_iterator begin() const
{ return Triangular_iterator( _beg_pos ); }
请问
return Triangular_iterator( _beg_pos ); 这句话的语法是怎么样的?
Triangular_iterator是自定义的一个class吧,class后面直接加(_beg_pos)什么意思
呢?
如果是返回一个Triangular_iterator的object, 不需要定义成
Triangular_iterator obj(_beg_pos);
return obj;吗?
谢谢!
================================Full Class definition==================
class Triangular_iterator
{
public:
Triangular_iterator( int index ) : _index( index-1 ){} //***
// other member functions
private:
void check_integrity() const;
int _index;
};
class Triangular {
friend class Triangular_iterator;
public:
Triangular( int len = 1, int bp = 1 );
Triangular( const Triangular& );
Triangular& operator=( const Triangular &rhs );
int length() const { return _length; }
int beg_pos() const { return _beg_pos; }
int elem( int pos ) const;
void length( int nlen ) { _length = nlen; }
void beg_pos( int npos ){ _beg_pos = npos; }
bool next( int &val ) const;
void next_reset() const { _next = 1; }
static bool is_elem( int );
static void gen_elements( int length );
static void gen_elems_to_value( int value );
static void display( int length, int beg_pos, ostream &os = cout );
typedef Triangular_iterator iterator;
Triangular_iterator begin() const
{ return Triangular_iterator( _beg_pos ); }
Triangular_iterator end() const
{ return Triangular_iterator( _beg_pos+_length ); }
private:
int _length; // number of elements
int _beg_pos; // beginning position of range
mutable int _next; // next element to iterate over
// static const int _max_elems = 1024; // ok, but not VC++
enum { _max_elems = 1024 };
static vector _elems;
}; | f*******n 发帖数: 12623 | 2 return Triangular_iterator( _beg_pos );
和
Triangular_iterator obj(_beg_pos);
return obj;
一样效果 | k*******3 发帖数: 1909 | 3 thanks
【在 f*******n 的大作中提到】 : return Triangular_iterator( _beg_pos ); : 和 : Triangular_iterator obj(_beg_pos); : return obj; : 一样效果
|
|