My C++ framework has Buttons. A Button derives from Control. So a function accepting a Control can take a Button as its argument. So far so good.
I also have List<T>. However, List<Button> doesn’t derive from List<Control>, which means a function accepting a list of Controls can’t take a list of Buttons as its argument. This is unfortunate.
Maybe this is a stupid question, but I don’t see how can I solve this 🙁 List<Button> should derive from List<Control>, but I don’t see a way to make this happen ‘automatically’.
I hate to tell you but if you’re using a list of instances to Control instead of pointers to Control, your buttons will be garbage anyway (Google ‘object slicing’). If they’re lists of pointers, then either make the
list<button*>intolist<control*>as others have suggested, or do a copy to a newlist<control*>from thelist<button*>and pass that into the function instead. Or rewrite the function as a template.So if you previously had a function called doSomething that took a list of controls as an argument, you’d rewrite it as: