Re: How can i return an iterator from a fucntion?
TOMERDR wrote:
ok suppose i will use what you suggest:
Please learn to quote correctly on Usenet. See
http://en.wikipedia.org/wiki/Top-post#Inline_replies for more
informations and http://cfaj.freeshell.org/google/ if you are using
Google Groups.
Appointment& FindAppointment(Duration d)
You might want to pass 'd' by const reference if it is not a [typedef
for a] built-in.
{
map<time_t,Appointment>:: iterator itor = ...;
You might want to typedef the map, it will make such usage much mroe
easy.
if (itor != m_Appointments.end())
return itor->second;
throw appointment_not_found();
}
Is it safe to call it from a loop?
Of course, why not?
while(continue){
'continue' is a keyword.
try {
Appointment app=FindAppointment(GetDuration())
FindAppointment returns a reference, don't copy objects when it is not
necessary.
Appointment& app=FindAppointment(GetDuration());
AddApointmetToContainer(app)
}
catch(appointment_not_found e)
Always catch by reference:
catch(appointment_not_found& e)
{
//do nothing
Err.. are you sure? In this case, just return a pointer instead of a
reference and return 0 if the value was not found. Although it may be
normal in your design not to find some appointments, it certainly looks
weird.
}
}
Last thing. Please remember that Usenet is a "public place". What you
write is read by many people. Try to respect netiquette and try to post
valid code (at least, end your statements with semicolons!)
Jonathan