Thread: help generic list and temlate

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    Question help generic list and template

    I want to create a generic list of objects.
    I could specify the list type ie

    typedef Plorg Item;

    Instead I want it to be generic (the type of list) and based on an object sent to the function create, which would create a list of the object refrenced. One alternative is too create a constructor of list type that resolves to a list of type any object passed to it.
    How do I resolve Any to the type of object passed either in the function create or a constructor of the List class.

    Code:
    // List of customers prblm 3 pg 417 c++ primer plus
    #ifndef _ListH_H_
    #define _ListH_H_
    
    
    template <typename Any>  // typename any
    void create(Any &a, Any &b);
    
    typedef Any Item; // is this valid?
    
    class List
    {
        private:
    	    enum {MAX=10};
    	    Item items[MAX];
    	    int index;
    	public:
            List();
    
            bool isempty() const;
    
            bool isfull() const;
            
            // add returns false if List is full, else true
            bool add(const Item &item); // add item to List
            
            // edit list function pointer
            void List::edlist(void (*pf)(Item &))
    };
    
    class Plorg
    {
    	private:
    		char name[19];
    		// ci--contentment index
    		int ci;
    	public:
    		Plorg();
    		void eat();
    		void report();
    };
    #endif
    Last edited by rip1968; 04-09-2002 at 06:22 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    Question would this work?

    Would this work?
    Passing an object to the constructor..................prbly not.
    Item needs to resolve to the type of object passed to the constructor. How do I do this?

    in main I would then do somthing like this
    Plorg Creature;
    List PlorgList(Creature);


    Code:
    // List of customers prblm 3 pg 417 c++ primer plus
    #ifndef _ListH_H_
    #define _ListH_H_
    
    
    template <typename Any>  // typename any
    void create(Any &a, Any &b);
    
    typedef Any Item;
    
    class List(const Item &item)
    {
        private:
    	    enum {MAX=10};
    	    Item items[MAX];
    	    int index;
    	public:
            List();
    
            bool isempty() const;
    
            bool isfull() const;
            
            // add returns false if List is full, else true
            bool add(const Item &item); // add item to List
            
            // edit list function pointer
            void List::edlist(void (*pf)(Item &))
    };
    
    class Plorg
    {
    	private:
    		char name[19];
    		// ci--contentment index
    		int ci;
    	public:
    		Plorg();
    		void eat();
    		void report();
    };
    #endif

Popular pages Recent additions subscribe to a feed