Thread: Linked Lists and Inheritance

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    4

    Linked Lists and Inheritance

    hi can any of you very intelligent people help me?. I am trying to make a singly linked list of class instances. I can do this with one class definition. However I have an abstract base class, four derived classes and two doubly derived classes. Instances of each of these must all be included on the same list!!. I have already written code to define each class along with the functions associated with each class. This is in 14 different files so I have not posted it here. After that... I am stuck. If anyone could help me , or knows any tutorials dealing with linked lists and inheritance. I have tried three books and they don't mention it at all!. My computer teacher at school was also very vague when I asked him...... This makes me think it must be a very hard thing to explain or understand. Do I need to use separate linked lists for each derived class(I tried this and it works, but is massively complicated!!). Any code, links, help at all would be brill!, but I know it is very difficult so I won't expect miracles :O)

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    4

    by the way..

    In the message below I have not posted any code. But if anyone does want to see the mess that is my code I can e-mail it to them. It seriously is waaaaaaaaay too long to post here!!.

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Make a templated class with a member that is a pointer to the specified type. In that templated class, make a templated member function (that's 2 layers of templating) which deletes the previous child instance and dynamically allocates a new instance of the specified type (make sure you have a virtual destructor on the base class).

    Then, make a single linked list of that type (with the corresponding base class as the template parameter), and when you want to set the child type, call the templated member function.

    Voila!

    Quick example of a class similar to what I'm describing

    Code:
    template< typename BaseType >
        class PolymorphismHandler
    {
    public:
        PolymorphismHandler()
            : DynamicInstance( 0 ) {}
        ~PolymorphismHandler() { delete DynamicInstance; }
        template< typename ChildType >
            ChildType& SetType()
        {
            delete DynamicInstance;
            return *dynamic_cast< ChildType* >( DynamicInstance = new ChildType );
        }
        BaseType& operator()() { return *DynamicInstance; }
        const BaseType& operator()() const { return *DynamicInstance; }
    protected:
        BaseType* DynamicInstance;
    };
    And then make the list out of those

    IE

    Code:
    SinglyLinkedList< PolymorphismHandler< MyBaseClass > > MyList;
    
    Link& CurrentLink = MyList.AddLink();
    
    CurrentLink.Data().SetType< MyChildType >();
    That of course all depends on if you have a good compiler. MSVC++ 6.0 for example, chokes on the above code despite the fact that it is completely valid C++ code.
    Last edited by Polymorphic OOP; 02-01-2003 at 07:09 PM.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    4

    Angry

    Is this the only way to do it, using templates??. I kind of understand what you mean but....if I have a lot of data in each instance will it be ok?. I have never used templates before...

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    No, you don't have to use templates. I just used templates there so you could use it with any datatype and never have to manually remake any functions. If you don't understand templates, I recommend you learn them, if not, then just get rid of them from my example.

    Code:
    class PolymorphismHandler
    {
    public:
        PolymorphismHandler()
            : DynamicInstance( 0 ) {}
        ~PolymorphismHandler() { delete DynamicInstance; }
        ChildType1& SetAsChildType1()
        {
            delete DynamicInstance;
            return *dynamic_cast< ChildType1* >( DynamicInstance = new ChildType1 );
        }
        ChildType2& SetAsChildType2()
        {
            delete DynamicInstance;
            return *dynamic_cast< ChildType2* >( DynamicInstance = new ChildType2 );
        }
        BaseType& operator()() { return *DynamicInstance; }
        const BaseType& operator()() const { return *DynamicInstance; }
    protected:
        BaseType* DynamicInstance;
    };
    Without templates, you'd have to remake the PolymorphismHandler class for each different base type (here you'd only need 1 anyways, but you make it templated so in case you come across a similar situation later with a different type). You'd also have to make a new "SetType" function for each different child type if you don't use templating, which would be very tedious.

    Again, I really recommend you learn templating for this instead.

    With templating, you can make more and more child-types and never have to manually change the PolymorphismHandler class to account for them.
    Last edited by Polymorphic OOP; 02-01-2003 at 07:34 PM.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    4
    Would you have to use this before you defined your classes?. I have all my classes and inherited classes defined already, and a basic linked list made, I just need to incorporate them into the list. Would I just need one linked list and different node types for each derived class?.

    class link
    {
    public:
    link();
    ~link();

    void SetNextLink(link *p) {nextlink=p;};
    link * GetNextLink(void) {return nextlink;};
    classname*GetClassnamePtr(void) {return &p}

    private:
    link * nextlink;
    classname p;
    };


    This is the kind of "generic" link I have. Could I fill in each derived classname for "classname" and have a couple of different "types" of link?. I cannot create a link for the base class because it is abstract and therefore "baseclassname p" gives me errors!. My problem then if I did that is.. how does the list "know" which type of link I want to insert??. I am so confused!!

    This is kind of a general link class for my linked list

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Nono, you're missing the point of the PolymorphismHandler class

    You don't need different types of links.

    classname p

    would just simply be replaced by

    PolymorphismHandler< classname > p;

    // ^ That's with templating.

    If you used the version that didn't have templating, it would just be

    PolymorphismHandler p;

    But once again, I really suggest used templating, and also template your linked list class as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-15-2001, 03:59 PM