DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  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 06: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, 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

Bookmarks

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


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


Sponsored Links