-
Inserting different types of objects into one queue
Hi,
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:
Queue<A> qobject;
B bob;
C cat;
qobject.insert(bob);
qobject.insert(cat);
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 06:51 PM.
-
Actually, what you're looking for is a heterogeneous conatiner. I discussed it recently:
http://www.devx.com/cplus/10MinuteSolution/29757
The pointer cast option is applicable only if you a hierarchy of classes, i.e., B is derived from C
Danny Kalev
Similar Threads
-
Replies: 4
Last Post: 08-14-2005, 01:03 PM
-
Replies: 2
Last Post: 01-15-2003, 01:46 PM
-
By Jeff Pipes in forum .NET
Replies: 11
Last Post: 05-15-2002, 10:17 AM
-
By Tahui in forum VB Classic
Replies: 2
Last Post: 11-22-2000, 10:24 PM
-
By Pedro Morais in forum VB Classic
Replies: 1
Last Post: 06-10-2000, 01:50 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks