tags:

views:

39

answers:

1

Having class :

    template<class T>
    class Link
    {
        Link* myParent_;
        Link* myLeft_;
        Link* myRight_;
        T* myData_;
        void assign_(Link<T>*& marker, Link<T>*& aLink);
        void insert_(const T&);//inserts new data into a link
        void insert_(const T*);
        void remove_();//removes data from a link
    public:

        class Iterator : public iterator<std::bidirectional_iterator_tag, Link<T>*>
        {
        private:
            Link<T>* myData_;//How can I assign object of external class to this link? Rest of the Q below.
        public:
            Iterator();
            Iterator& left()const;
            Iterator& right()const;
            Iterator& top()const; 
        };
};

What I mean by that is how can I assign "this" object to myData_ and not myLeft_, myRight_ or myParent_ of "this" object?

I tried something like this:

template<class T>
Link<T>* Link<T>::me() const
{
    return const_cast<Link<T>*>(this);
}

and in Iterator:

template<class T>
Link<T>::Iterator::Iterator():myData_(nullptr)
{
    myData_ = me();//call from external class to me();
}

but I'm getting an error:

Error 1 error C2352: 'Link::me' : illegal call of non-static member function

Thank you.

+1  A: 

Either via constructor:

Iterator(Link<T> *l) : myData_(l)  {}

Or via a setter and getter:

void setData(Link<T> *d)  { myData_ = d; }
Link<T>* getData() const { return myData_; }
dark_charlie
Get/Set(er) are a code smell. The interface to your class should be actions that can be performed on the object not an interface that allows you to muck with the underlying data structures.
Martin York