Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insertion of different types of objects into one queue

Status
Not open for further replies.

raydona

Programmer
May 12, 2005
27
0
0
GB
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
 
Raydona,

Thats why the class is templated. In Java you can insert classes of type Object into queues and then typecast them to their original type. In C++ a queue can only hold one type of object. Now what you might want to do is create another object which has two fields. One for classname and one for the actual object. Then load your objects into this object and store the new object in the queue;


Code:
[blue]
class myObject
{
   string m_Classname;   
   C m_CObject;  [green]//Assuming you can manipulate your classes to all be casted to type C object.  If you can
t, you may be able to try to cast the Objects to type A but unlike Java I believe C++ will destroy the fields that are not in type C and B that are in type A if you cast them to A[/green]
  
   myObject();

};  

[green]//Default Constructor[/green]
myObject::myObject(C aObject, string aClass)
{
   m_CObject = aObject;
   m_Classname = aClass;
} 


C::getObject(Queue<C> myQueue)
{
  [green]//Something along the lines of [/green]
  if (aClassname=="A")  return (A)(myQueue.getObject());
}


[/blue]


Very Brief Code, but that is just to get you started.

Hope that gives you some idea.

-Ron

typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
Since B and C are both derived from A, you can store them in the same queue, and ronnyjljr's code isn't necessary.

What you need to do is store pointers to the base class in your queue. If you do so, you won't slice off any of the derived-ness of the B and C instances.

If necessary, you can dynamic_cast back to B or C later, although that is usually a sign of poor design, using polymorphism is generally a better idea.
Code:
Queue<A*> qobject;
qobject.insert(new B);
qobject.insert(new C);
That is the basics. It would be a good idea to use a shared smart pointer (like boost::shared_ptr but not std::auto_ptr) or maybe look into boost::ptr_container. However, if you manage your memory properly a raw pointer will work as well.

If you want to hold to unrelated classes in the same container, then boost::variant or boost::any might work well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top