Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple objects in a vector...

 
amr amr
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey i hope you can help me with this...,
I have a vector which contains several elements consisting of a user defined object. Now I'd like to add objects of ANOTHER user defined object. I know you can do this in C++, but can you also do this in java? My main question is once I add elements of this new objectt ype, how do I go through the vector element by element checking to see what type of object is stored in there. If I have the first user defined object in the slot, then I need to do a certain set of operations to it and if I have the 2nd user defined object in the slot then I need to do another set of operations to it....thanks for all the help!
amr
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
amr amr
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help but it still hasnt answered my question...perhaps I wasnt clear...I want to know how exactly you check to see what type of object is stored in the element...in other words in those if statements in the code how do i determine if elm is an instance of class1 or class2....i hope this clarifies my question better...thanks again...
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amr amr:
thanks for the help but it still hasnt answered my question...perhaps I wasnt clear...I want to know how exactly you check to see what type of object is stored in the element...in other words in those if statements in the code how do i determine if elm is an instance of class1 or class2....i hope this clarifies my question better...thanks again...


To check the type of an object use the instanceof operator, I guess you missed it up in Jean-Francois post.

Originally posted by Jean-Francois Briere:

// verify its class type
if (elm instanceof ClassName1)
{
ClassName1 elm1 = (ClassName1)elm;
...
}

[ March 28, 2004: Message edited by: Vicken Karaoghlanian ]
 
amr amr
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ok thanks, i thought it was pseudocode or something, i will try that...do i also need the elm before instane of that he has in the if statement
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic