Inserting different types of objects into one queue

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Inserting different types of objects into one queue

  1. #1
    Join Date
    May 2005

    Arrow 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:

    Queue<A> qobject;
    B bob;
    C 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

  2. #2
    Join Date
    Dec 2003
    I would make a union or a class with pointers to all the classes and make a Q of that.

  3. #3
    Join Date
    Dec 2004
    San Bernardino County, California
    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.

  4. #4
    Join Date
    Nov 2003
    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
    Danny Kalev

Similar Threads

  1. Queue Stack Question
    By trixma in forum Java
    Replies: 4
    Last Post: 08-14-2005, 01:03 PM
  2. Replies: 2
    Last Post: 01-15-2003, 01:46 PM
  3. Pooling in n-tier architecture
    By Jeff Pipes in forum .NET
    Replies: 11
    Last Post: 05-15-2002, 10:17 AM
  4. What is the UDT?
    By Tahui in forum VB Classic
    Replies: 2
    Last Post: 11-22-2000, 10:24 PM
  5. Problems when Inserting Objects in DataBase - Urgent
    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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center