struct Database
{
String _name;
int _age;
String _sex;
String _email;
String _eid;
String _address;
double _percentage;
};
class MyStudentDatabase
{
public: MyStudentDatabase();~MyStudentDatabase();
void addData(String name, int age, String sex, String email, String eid, String address, double percentage);
private: int _iSize;
Database * _pStructObject;
};
ここに定義部分があります -
MyStudentDatabase::MyStudentDatabase()
: _iSize(1)
{
_pStructObject = new Database[_iSize];
}
MyStudentDatabase::~MyStudentDatabase()
{}
void MyStudentDatabase::addData(String name, int age, String sex, String email, String eid, String address, double percentage)
{
_pStructObject[_iSize - 1]._name = name;
_pStructObject[_iSize - 1]._sex = sex;
_pStructObject[_iSize - 1]._email = email;
_pStructObject[_iSize - 1]._eid = eid;
_pStructObject[_iSize - 1]._address = address;
_pStructObject[_iSize - 1]._percentage = percentage;
}
このプログラムは、構造体のサイズが であるため、一度だけデータを保存するため_iSize(1)
、_pStructObject[0]
すべてのメンバーに対して機能します。別のメンバーを追加して、そのaddData(...)
関数のサイズを_iSize++
; しかし、問題は、そうすると、コンストラクター内で「新しい」演算子を使用すると、以前のデータが失われることです。これに対する解決策はありますか?