运算符的重载
元明 2000-03-17 10:24:00 运算符的重载我不能很好的理解,到底都能应用在什末地方,请尽量通俗些,以以下的程序为例.
#include <stream.h>
typedef enum{red,green,amber} traffic_light_color;
struct intersection
{
traffic_light_color traffic_light;
int number_cars_queued;
int cumulative_numbers_cars;
friend ostream &operator << (ostream &s, intersection &light);
};
ostream &operator << (ostream &s, intersection &light)
{
switch(light.traffic_light)
{
case red: return (s << "red");
case green: return (s << "green");
case amber: return (s << "yellow");
}
}
intersection network[50];
main()
{
netword[1].traffic_light = amber;
cout << "\nThe light at the 2nd intersection is" << network[1];
}