R
Rex_chaos
Hi there,
I am writing an iterator for a container. Just like a typical
iterator,
template <typename ADT>
class MyIter
{
...
MyContainer<ADT>& refc;
public:
MyIter(MyContainer<ADT>& cont) :refc(a) {...}
MyIter& begin() {...; return *this;}
MyIter& end() {...; return *this;}
MyIter& operator++() {...; return *this;}
...
};
While MyIter is applied to a template algorithm like std::transform,
the copy constructor will be actived because of the reference to cont.
It lowers effeciency a lot. Maybe I should use a pointer as instead
for speed purpose
....
MyContainer<ADT>* refc;
public:
MyIter(MyContainer<ADT>& cont) :refc(a) {...}
....
However, it is another problem. Suppose the container passed to the
iterator has been released, refc will then become an invalid pointer
!!! I can't release the pointer (refc) in the destructor because I
don't know if any object own the container.
I am writing an iterator for a container. Just like a typical
iterator,
template <typename ADT>
class MyIter
{
...
MyContainer<ADT>& refc;
public:
MyIter(MyContainer<ADT>& cont) :refc(a) {...}
MyIter& begin() {...; return *this;}
MyIter& end() {...; return *this;}
MyIter& operator++() {...; return *this;}
...
};
While MyIter is applied to a template algorithm like std::transform,
the copy constructor will be actived because of the reference to cont.
It lowers effeciency a lot. Maybe I should use a pointer as instead
for speed purpose
....
MyContainer<ADT>* refc;
public:
MyIter(MyContainer<ADT>& cont) :refc(a) {...}
....
However, it is another problem. Suppose the container passed to the
iterator has been released, refc will then become an invalid pointer
!!! I can't release the pointer (refc) in the destructor because I
don't know if any object own the container.