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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

static_cast iterator usage 1

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Hello,
In the Java world, when using an Iterator over a container you have to cast the object to it's appropriate type, like this:

Code:
ArrayList list = new ArrayList(); //Note this does not use generics from Java 1.5
list.add("Hi");
list.add("Bye");
Iterator it = list.iterator();
while (it.hasNext())
{
   String strTmp = (String) it.next();
}

Is it necessary to use static_cast on an STL iterator in C++? Example:
Code:
std::vector<CString>::iterator it     = list.begin();
std::vector<CString>::iterator it_end = list.end();
while (it != it_end)
{
   CString strTmp = *it; 
   // or is this correct?
   CString strTmp = static_cast<CString>(*it);
   it++;
}

Both compile and work, but is the static_cast necessary since the compiler already knows the type?


thanks for your help,


Barry
 
No, the static_cast is not necessary.
Code:
CString strTmp = *it;
is fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top