Thread: passing an array of templated objects

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    passing an array of templated objects

    Hello, im trying to create a function which recieves an array (by reference) of templated objects but i am unsure of the syntax. ive tried the following, none work

    (ex)

    template <class T>
    void PrintList( ItemList<T>[] & list )

    template <class T>
    void PrintList( ItemList<T> & list[] )

    template <class T>
    void PrintList( ItemList[]<T> & list )

    if anyone can help id be very greatful. thank you!

    -mike

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    good call with the array reference. i retried

    template <class T>
    void PrintList( ItemList<T> & list[] ) without the reference:

    void PrintList( ItemList<T> list[] )


    and it compiles. thanks for the reminder on arrays!

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >good call with the array reference.
    Thank you. I deleted my post after I realized you had a class template.
    But as you said the reference part was applicable.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I believe the closest thing to what you would want is a reference to a pointer, which I have never been able to get to work with automatic arrays just by themselves.

    Code:
    template <class T>
    	class F
    	{
    		friend std::ostream & operator << (std::ostream &, F<T> &);
    		
    		public:
    			F(void) : _t(0) { }
    			F(T t) : _t(t) { }
    			void printT(void) { std::cout << _t; }
    		private: T _t;
    	};
    	
    
    template <class T>
    	void rawr(F<T> *& l, size_t s)
    	{
    		for(size_t i = 0; i < s; ++i)
    			l[i].printT();
    	}
    	
    int main()
    {
    	F<int> * fP = new F<int>[10];
    	F<int> fA[] = { 0, 1, 2, 3, 4 };
    	F<int> * fPA = fA;
    	
    	rawr(fP, 10); std::cout << std::endl;
    	rawr(fA, sizeof fA / sizeof F<int>); // error
    	rawr(fPA, sizeof fA / sizeof F<int>); // okay
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    nope, doesnt work when linking object files. let me try to be more clear what im trying to do.

    i need to pass an array of templated objects ListItem<T>. declaration looks like this (for example):

    Code:
    ListItem<Fruit> item[5];
    (an array of 5 List Items of <Fruit>)

    and i have to determine what parameter is used for a function recieving this array. my question is -

    if the syntax for an array of ints is:

    Code:
    void PrintList( int[] list )
    what would an array of templated ListItem<T> objects be?? any idea?? thanks

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Looks exactly like what I did. What was the error that you got. i told you that I could not figure out how to pass the automatic array identifier directly, but that I could pass a pointer to the array in my previous post.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    oh im sorry, i was referring to my first method. anyways, i did not write main(where the function call is made), nor can i alter it. it is passing the array:

    (ex)

    ListItem<Fruit> items[5];
    PrintList( items );

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You need to pass the size along. Or REALLY pass a reference to the array:
    Code:
    template<typename T, unsigned int N>
    void PrintList(ListItem<T> (&items)[N])
    Then you can use N as the size. But this generates a separate function for every size you pass. Good for optimization, bad for code size, and you can't pass dynamically allocated arrays.

    In general, it's a much better idea to use STL containers anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help passing nxn array to 2d array.
    By beglaryanh in forum C Programming
    Replies: 2
    Last Post: 06-06-2009, 05:23 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. Passing objects
    By Wiggin in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2001, 08:00 AM