怎样让一个string成员自动编号
TICPP的一道题目
我用stringstream做出来了,就是如果
ss << "Bird #" <<Bird::i; //注意,Bird和#中间有一个空格ss >> name;
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class Bird {
string name;
static int i;
public:
Bird(){
Bird::i++;
stringstream ss;
ss < < "Bird#" < <Bird::i;
ss >> name;
}
Bird(const Bird& obj) {
Bird::i++;
name = "copyed from " + obj.name;
}
friend ostream& operator < <(ostream& os, const Bird& obj) {
os < < obj.name;
return os;
}
const Bird operator=(const Bird& obj) {
Bird::i++;
name = "assigned from " + obj.name;
return *this;
}
};
int Bird::i = 0;
int main() {
Bird o1;
cout < < o1 < < endl;
Bird o2;
cout < < o2 < < endl;
o2 = o1;
cout < < o2 < < endl;
Bird o3(o2);
cout < < o3 < < endl;
}
Bird(){ Bird::i++; stringstream ss; ss << "Bird#" <<Bird::i; name=ss.str(); }