Ostream& operator<<() multiple outputs

My question is I have a simple class that creates a contact object (First name, last name, phone, email). I have created the class and I'm using STL functions to create a container of these contacts, but depending on what function it do i.e (add, remove, list, find) the output is different (remove << first and last name, find << all the data in contact.

My question is if there is a way to have ostream& operator<< output depending on a case. I feel that there isn't and I will just have to output the strings like.

 
  cout << contact.last << contact.first;


I feel the teacher wants me to use

 
 cout << it->first << it->second ;


Since we are using containers.

I hope this makes sense.

Well, you can overload the operator yourself and then you'll have full control over what it does. If you don't wait it to print if fails a certain condition, then you can just not print anything. Or you can just have special output inside each of the functions themselves.

The syntax from the second snippet is using pointers to objects, while the first is using the objects themselves. Both can do the same thing.
Topic archived. No new replies allowed.