c++ - struct reference and operator= -


i have class this:

template <class t>     class bag     {     private:             typedef struct{t item; unsigned int count;} body;         typedef struct _node{_node* prev; body _body; _node* next;}* node;           struct iterator{             enum exception{notdefined, outoflist};             body operator*();              explicit iterator();             explicit iterator(const iterator&);             iterator& operator=(const iterator&);             iterator& operator++(int);             iterator& operator--(int);             bool operator==(const iterator&) const;             bool operator!() const;              private:             node current;              friend class bag;         };         node head;         node foot;          iterator _begin;         iterator _end;          /* ... */      public: /* ... */         bag();         const iterator& begin;         const iterator& end;             }; 

in bag() have set reference begin _begin, , end _end.

begin = _begin; end = _end; 

but think line

begin = _begin; 

invokes bag::iterator::operator=() function.

how can avoid that?

references can't assigned, initialised. need initialise them in constructor's initialisation list:

bag() : begin(_begin), end(_end) {} 

however, it's more conventional (and reduces class size) these using accessor functions rather public references:

const iterator& begin() {return _begin;} const iterator& end() {return _end;} 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -