第1个回答 2013-07-21
int cs_temp_times=0;
class cs_temp{
public:
cs_temp(){
cout<<"[ cs_temp:cs_temp() ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
}
cs_temp(const cs_temp & ){
cout<<"[ cs_temp:cs_temp(const cs_temp & ) ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
}
cs_temp& operator=(const cs_temp &){
cout<<"[ cs_temp:operator =(const cs_temp &) ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
return *this;
}
operator string(){
cout<<"[ cs_temp:type to string ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
return string("[ cs_temp:type to string ]");
}
operator bool(){
cout<<"[ cs_temp:type to bool ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
srand(_time32(NULL)+rand());
return (rand()&0x1);
}
operator int(){
cout<<"[ cs_temp:type to int ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
srand(_time32(NULL)+rand());
return rand();
}
cs_temp operator++(int){
cout<<"[ cs_temp:operator p++ ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
return cs_temp();
}
cs_temp operator+(const cs_temp &){
cout<<"[ cs_temp:operator +(const cs_temp &) ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
return cs_temp();
}
cs_temp operator-(const cs_temp &){
cout<<"[ cs_temp:operator -(const cs_temp &) ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
return cs_temp();
}
cs_temp operator*(const cs_temp &){
cout<<"[ cs_temp:operator *(const cs_temp &) ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
return cs_temp();
}
cs_temp operator/(const cs_temp &){
cout<<"[ cs_temp:operator /(const cs_temp &) ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
return cs_temp();
}
cs_temp operator&(const cs_temp &){
cout<<"[ cs_temp:operator &(const cs_temp &) ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
return cs_temp();
}
cs_temp operator|(const cs_temp &){
cout<<"[ cs_temp:operator |(const cs_temp &) ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
return cs_temp();
}
cs_temp& operator++(){
cout<<"[ cs_temp:operator ++p ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
return *this;
}
~cs_temp(){
cout<<"[ cs_temp:~cs_temp() ]"<<"ID:"<<this<<" times:"<<cs_temp_times++<<endl;
}
};
第2个回答 2013-07-21
就是说你定义了一个新的类,比如new_class它有两个成员int english和int math。然后在类里又进行了‘+’的操作符重载,定义为englsih和math分别相加。现在定义new_class var1,var2。然后表达式var1+var2就表示把它们的english和math分别相加
第3个回答 2013-07-21
tamplate <typename T>或者tamplate <class T>
T yourExampleName(T a, T b)
{...}