c++ - How I return something else when a function returns class data member? -
accountholder search(int accno, string cnic, accountholder* customer, int counter) { (int = 0;i < counter;i++) { if (account == customer[i].getaccno() && cnic == customer[i].getcnic()) { return customer[i]; } } return (-----); }
the problem want return tells me account number , cnic not match customer id. can creating new object , constructor sets data members 0 or empty string, may able check such customer not present there other way? it's first question, hope haven't asked stupid question. thanks!
there many ways of doing this, nicer others depends on situation.
if doesn't happen , actual error (this key, slow), throw exception.
you instead return const pointer object, , return null in case of error. makes sense if original object guarenteed not disappear during usage.
you instead return bool , take accountholder reference , fill in if there match.
you take boolean reference, don't option quite strange interface choice.
i'm sure there other choices.
Comments
Post a Comment