Casting std::lists by value type

This is a discussion on Casting std::lists by value type within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, been using this forum as a resource for quite awhile now. I have almost always been able to ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    10

    Casting std::lists by value type

    Hi everyone, been using this forum as a resource for quite awhile now. I have almost always been able to find a solution to my problems here
    But i have a problem now i could really use some help with.

    Say i have two classes defined something like this.

    Code:
    class base //abstract
    {
    public:
    	base() {};
    	virtual void foo() = 0;
    };
    
    class top: public base
    {
    public:
    	top() {};
    	virtual void foo() {}
    };
    And i need to call a method that is declared like this
    Code:
    void myFunc( std::list<base*>)
    Supplying as argument, a list of top-objects ( and/or top-pointers )

    Code:
    std::list<top*> top_list;
    or
    std::list<top> top_list;

    Example:

    Code:
    void myFunc( std::list<base*> list )
    {
    	//yada yada
    }
    int main()
    {
    	std::list<top> top_list;
    	myFunc( <insert some sort of cast here? > top_list );
    }
    The reason i want myFunc to take a list of the base-class is that i need to call it with lists containing various classes derived from base.

    How can i possibly achieve this without redesigning myFunc( ) to take one single object/pointer?
    Is it possible at all using the stl container classes?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You need to either start with a std::list<base*> even if it will only hold top*, or make a copy of the list to pass to the function (this is ok since you would be only copying pointers so the same objects would get passed into the function):
    Code:
    #include <list>
     
    class base //abstract
    {
    public:
    	base() {};
    	virtual void foo() = 0;
    };
     
    class top: public base
    {
    public:
    	top() {};
    	virtual void foo() {}
    };
     
    void myFunc( const std::list<base*>& list )
    {
    	//yada yada
    }
     
    int main()
    {
    	std::list<top*> top_list;
     
    	// ...
     
    	{ // Prefer destruction of tmpList immediately after function.
    		std::list<base*> tmp_list(top_list.begin(), top_list.end());
    		myFunc(tmp_list);
    	}
     
    	// ...
     
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. type casting object to int?
    By symbiote in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2009, 11:46 PM
  2. type casting void *
    By Alexpo in forum C Programming
    Replies: 5
    Last Post: 06-23-2008, 04:05 AM
  3. What dose this type casting mean??
    By zhoufanking in forum C Programming
    Replies: 4
    Last Post: 06-11-2008, 07:09 AM
  4. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  5. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21