(原)
#include
#include
#include
#include
#include
#include
using namespace std;
#define cout qDebug()
class Dog
{
public:
Dog()
{
_number = 0;
}
Dog(int number, const QString &name)
{
_number = number;
_name = name;
}
void setNumber(int number)
{
_number = number;
}
int number() const
{
return _number;
}
void setName(const QString &name)
{
_name = name;
}
QString name() const
{
return _name;
}
private:
int _number;
QString _name;
};
QDataStream &operator<<(QDataStream &out, const Dog &dog)
{
out << dog.number() << dog.name();
return out;
}QDataStream &operator>>(QDataStream &in, Dog &dog)
{
int number = 0;
QString name;
in >> number >> name;
dog.setNumber(number);
dog.setName(name);
return in;
}
int main(int argc, char *argv[])
{
/*QFile file("data.dat");
Dog dog1(1, "caterpillar");
Dog dog2(2, "momor");
if(!file.open(QIODevice::WriteOnly))
{
cerr << "Cannot open file for writing: " << qPrintable(file.errorString()) << endl;
return false;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_3);
out << dog1 << dog2;
file.close();
if(!file.open(QIODevice::ReadOnly))
{
cerr << "Cannot open file for reading: " << qPrintable(file.errorString()) << endl;
return false;
}
QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_3);
in >> dog1 >> dog2;
cout << dog1.number() << ", " << qPrintable(dog1.name()) << endl
<< dog2.number() << ", " << qPrintable(dog2.name()) << endl;*/
QList list;
Dog dog1(1, "caterpillar");
Dog dog2(2, "momor");
list << dog1 << dog2;
QList::const_iterator iterator = list.begin();
while(iterator != list.end()) {
cout << (*iterator).number() << ", "
<< qPrintable((*iterator).name()) << endl;
++iterator;
}
return true;
}
阅读(1599) | 评论(0) | 转发(0) |