简体   繁体   中英

Calling iterator constructor from another class

I am trying to write graph class as a template class. As answered in Question , I was trying to implement graph in term of std::set , here is what I have wrote till now.

    #include <set>

    template <class T, 
            class Container = std::set<T> >
    class graph {
    public:
        class iterator { 
        public:
            iterator() {
                std::set<T>::iterator();
            }
            iterator(const iterator&) {
                std::set<T>::iterator();
            }
            ~iterator() {
                std::set<T>::~iterator();
            }

What I am looking for here is whenever graph::itertor is called, internally it should called set::iterator , Is this approach ok, and why ~iterator() is not getting compiled.

I think you want to provide iterators for your graph class, and graph's iterators are nothing but the iterators of the underlying container type. If so, then you should actually be doing this,

template <class T,  class Container = std::set<T> >
class graph 
{
public:
    typedef typename Container::iterator iterator;
    typedef typename Container::const_iterator const_iterator;

    //...
};

As for your code, I would say that std::set<T>::iterator() creates a temporary object, and the temporary gets destroyed at the end of the full expression. It's story ends there. And std::set<T>::~iterator() wouldn't even compile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM