开发者

Template Friend for Superclass

I have a non-templatized class (Par_list_elem), and I would like to give access to its internals to the class Par_list (to build an intrusive list).

The catch: I need Par_list_elem and all of its subclasses to be accessible to Par_list. In detail, 开发者_StackOverflow社区the only fields that need to be accessible are _next and _prev; explicitly limiting to those fields would be nice, but isn't required.

I've made a few preliminary attempts at doing this, the latest of which is below:

        template <class T> class Par_list {
            public:

                    Par_list() : _head(0) {}
                    ~Par_list();

                    //Insert element into list
                    bool insert(T elem);


                    //Remove element identified by iterator
                    void erase(iterator itr);

            private:
                    T* _head;
    };


    class Par_list_elem {
            public:
                    Par_list_elem() : _next(0), _prev(0) {}
                    //Get next element in list
                    Par_list_elem* next() { return _next; }

            private:
                    Par_list_elem* _next;
                    Par_list_elem* _prev;

                    template <typename> friend class Par_list;
    };




    template <class T> void Par_list<T>::erase(Par_list<T>::iterator itr) {

        T* e = *itr;

        T* p;
        if ((p = e->_prev) != 0)
                p->_next = e->_next;
        else
                _head = e->_next;

        if ((e->_next) != 0)
                (e->_next)->_prev = p;

        delete e;
    }

    template <class T> bool Par_list<T>::insert(T* nelem) {

        T* curr = _head;
        if (curr != 0)  {
                while (curr->_next != 0)
                        curr = curr->next();
                curr->_next = nelem;
        } else
                _head = nelem;

        nelem->_prev = curr;
        nelem->_next = 0;

        return true;
}

test.cpp

#include "parsnip_list_back.h"

class elem : parsnip::Par_list_elem {
    int _elem;
};

int main (int argc, char** argv) {

    parsnip::Par_list<elem> plist;

    return 0;
}

Some information seems to be available here: Template friend But the goal is different in enough detail that I'm stuck.

Thanks!

--------UPDATE---------

The following sort of error occurs for each instance of an access of a private member of Par_list_elem. I'm compiling on gcc 4.4.

parsnip_list_back.h:66: error: ‘parsnip::Par_list_elem* parsnip::Par_list_elem::_prev’ is private
parsnip_list_back.h:124: error: within this context

So, with the current implementation, even the superclass isn't giving up its privates.


The problem is that you're inheriting privately from Par_list_elem. So, although Par_list can access private members of the base class, it cannot access the base class itself.

I need Par_list_elem and all of its subclasses to be accessible to Par_list.

This would solve the problem if it were possible, but it can't be done; friendship isn't inherited.

The simplest solution is to inherit publicly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜