titanandrews
Programmer
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:
Is it necessary to use static_cast on an STL iterator in C++? Example:
Both compile and work, but is the static_cast necessary since the compiler already knows the type?
thanks for your help,
Barry
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