STL inherit from container<T>::iterator

  • Thread starter Christoph Heindl
  • Start date
C

Christoph Heindl

Hi,

I'm currently struggling with a design decision. One of my classes
(named Attributable) has a protected member of type

std::list<StrongReference<Attribute> >

Basically the list has strong references that point to Attribute's.
In Attributable i want to provide an stl like interface for
bidirectional iterators like:

Iterator Attributable::attributes_begin();
Iterator Atrributable::attributes_end();

I could easily typedef Iterator as std::list said:
::iterator. However,
I certainly don't want to have the user of Attributable to access the
strong reference instance.
I rather want to hide the fact that there are strong references
maintained and only provide him access to the actual Attribute pointer.

To accomplish this, the only way that comes up into my mind is to
inherit from
std::list<T>::iterator and override the operators * and ->. However
since i don't have knowledge about internals of the
std::list<T>::iterator, I doubt that this is a good idea.

Any suggestions, links etc.?
Thanks in advance,
Christoph
 
D

Daniel T.

"Christoph Heindl said:
Hi,

I'm currently struggling with a design decision. One of my classes
(named Attributable) has a protected member of type

std::list<StrongReference<Attribute> >

Basically the list has strong references that point to Attribute's.
In Attributable i want to provide an stl like interface for
bidirectional iterators like:

Iterator Attributable::attributes_begin();
Iterator Atrributable::attributes_end();


I certainly don't want to have the user of Attributable to access the
strong reference instance.
I rather want to hide the fact that there are strong references
maintained and only provide him access to the actual Attribute pointer.

To accomplish this, the only way that comes up into my mind is to
inherit from
std::list<T>::iterator and override the operators * and ->. However
since i don't have knowledge about internals of the
std::list<T>::iterator, I doubt that this is a good idea.

Any suggestions, links etc.?

Inheriting from a standard iterator is a bad idea. It is likely that any
code you put in the class would be just as useful for any of the other
standard iterators (other containers could hold StrongReferences as
well. Best would be to write a class that contains the list::iterator
and have your class create one of them with the iterator inside it.
 
C

Christoph Heindl

hi Daniel,

thanks for hint. Seems like a very reasonable idea. I gonna give it a
try!

best regards,
christoph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top