Inserting different types of objects into one queue
I wish to place objects of class B and class C into one queue. The queue, say class Queue, is a class template. Further, B and C are derived from class A. Can I say something like:
or do I have to cast objects of B and C to type A before insertion. If so how do I cast objects from one type to another at run time.
Also, if the above is possible how do I, when removing objects from the queue, know which type of object I am removing.
I would be grateful for all help
I would make a union or a class with pointers to all the classes and make a Q of that.
couldn't you "cast" them at time of creation of the objects?
A* bob = new Bob;
A* cat = new Cat;
now the object pointer is of the supertype A but are constructed as objects of the child classes .. and they can be added to the queue ...
Last edited by nspils; 01-23-2006 at 07:51 PM.
Actually, what you're looking for is a heterogeneous conatiner. I discussed it recently:
The pointer cast option is applicable only if you a hierarchy of classes, i.e., B is derived from C
Last Post: 08-14-2005, 02:03 PM
Last Post: 01-15-2003, 02:46 PM
By Jeff Pipes in forum .NET
Last Post: 05-15-2002, 11:17 AM
By Tahui in forum VB Classic
Last Post: 11-22-2000, 11:24 PM
By Pedro Morais in forum VB Classic
Last Post: 06-10-2000, 02:50 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL