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
    Posts
    33

    Arrow 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

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    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
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    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

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


   Development Centers

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