CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2008
    Posts
    31

    Generic programming. Templates?

    Hi!

    I was hoping for some advice on how best to create a single doubly-linked list which could contain all of the different types of classes I have.

    This is the code I have written so far....

    shape.h:

    Code:
    class Shape
    {
    public:
    
    	Shape (int l=0, int h=0) : length(l), height(h) {}
    private:
    
    	int length;
    	int height;
    
    protected:
    
    };
    
    class Square : public Shape
    {
    public:
    
    private:
    	int side1;
    	int side2;
    	int side3;
    	int side4;
    }
    
    class Triangle : public Shape
    {
    public:
    
    private:
    
    	int side1;
    	int side2;
    	int side3;
    }
    main.cpp:
    Code:
    #include <iostream>
    #include "shape.h"
    
    using namespace std;
    
    int main()
    {
    
    	int x;
    	int y;
    
    	cout << "enter the heighe" << endl;
    	cin >> x;
    	cout << "enter he length" << endl;
    	cin >> y;
    
    	Shape square(x,y);
    
    	system ("PAUSE");
    	return 0;
    }
    I've read loads these last couple of days on doubly-linked lists, but am not understanding the role of Templates.

    Would use of a Template allow me to store the 3 different types of classes I have within a single doubly linked list?

    I code I have written concerns me as I am creating an object of type Shape, but I'm thinking I should be storing pointers to the objects, not the objects themselves within the list.

    If anyone can offer me some direction and advice on the matter it would be most appreciated

    Many thanks!

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Generic programming. Templates?

    Well if you are going to try to store multiple classes in a singly linked list you are going to have to handle the type and the size somehow. For example you could use a struct that contains:

    Code:
    typedef struct multi_list{
          struct multi_list *next;
          mybase *data;
          int type;//This describes the type
    };
    Now if you are going to try to do this with templates, it is another thing:

    Code:
    vector<bird>b(10);
    you will only use 1 class at a time w.standard STL containers.
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    May 2007
    Posts
    811

    Re: Generic programming. Templates?

    Some other options, you can store pointers in a stl container using boost::share_ptr or even better, one of boost containers. This will allow you to store pointers to your Shape classes.
    You can also use boost::any which can hold any kind of data.

  4. #4
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Generic programming. Templates?

    Quote Originally Posted by STLDude View Post
    Some other options, you can store pointers in a stl container using boost::share_ptr or even better, one of boost containers. This will allow you to store pointers to your Shape classes.
    You can also use boost::any which can hold any kind of data.
    Ah. Boost always has the answer, doesn't it?
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Generic programming. Templates?

    Quote Originally Posted by Etherous
    Ah. Boost always has the answer, doesn't it?
    Not always, and in this case std::tr1::shared_ptr may be available (but admittedly it is a progression from Boost).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Generic programming. Templates?

    The beauty of a linked list is that by its nature, you don't need to worry so much about type or size. One item in the list could be allocated from memory totally separate from the next. The sole linking factor between each member is the pointer to the next and previous items (NULL if beginning or end). It seems you want to make your own, even though there are plenty of options already out there. In the context of the code you've given, this is how I would solve it. (Note: Templates are not needed unless the shape variable types vary. We use polymorphism to the base Shape class for links. I'll focus only on the Shape class, since the others will simply inherit it):
    Code:
    class Shape
    {
    public:
        Shape (int l=0, int h=0) : length(l), height(h), next(NULL), last(NULL) {}
        virtual ~Shape ()
        {
            if(last)
                last->next = next;
            if(next)
                next->last = last;
        }
        Shape *getIndex (int index)
        {
            if(index == 0)
                return this;
            Shape *node = this;
            if(index < 0)
                for(int i = 0; (i > index) && node; i--)
                    node = node->last;
            else
                for(int i = 0; (i < index) && node; i++)
                    node = node->next;
            return node;
    
        }
        void add (Shape *INobj)
        {
            Shape *node = this;
            while(node->next)
            {
                // Cannot allow duplicates (infinite loop would result)
                if(node == INobj)
                    return;
                node = node->next;
            }
            node->next = INobj;
            INobj->last = node;
            INobj->next = NULL;
        }
        void remove (Shape *INobj)
        {
            Shape *node = this;
            while(node)
            {
                if(node == INobj)
                {
                    if(node->last)
                        node->last->next = node->next;
                    if(node->next)
                        node->next->last = node->last;
                }
            }
        }
        void remove (int index)
        {
            Shape *node = this;
            for(int i = 0; (i < index) && (node); i++)
                node = node->next;
            if(node)
            {
                if(node->last)
                    node->last->next = node->next;
                if(node->next)
                    node->next->last = node->last;
            }
        }
        void insert (int index, Shape *INobj);
        Shape operator + (int add)
        {
            if(add < 0)
                return operator-(-add);
            Shape *node = this;
            for(int i = 0; (i < add) && node; i++)
                node = node->next;
            return *node;
        }
        Shape operator - (int sub)
        {
            if(sub < 0)
                return operator+(-sub);
            Shape *node = this;
            for(int i = 0; (i < add) && node; i++)
                node = node->last;
            return *node;
        }
        Shape operator[] (int index) { return *getIndex(index); }
    private:
        int length;
        int height;
    
    protected:
        Shape *next;
        Shape *last;
    };
    PS: I removed your tabs, cause tabs are icky.

    Edit: laserlight's right. I kind of rushed this. Destructor has been made virtual
    It's also not const correct, but you get the idea
    Last edited by Etherous; March 28th, 2009 at 01:00 PM.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Generic programming. Templates?

    Incidentally, note that Shape should have a virtual destructor since it is a base class.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured