私は2つのクラスのWomanとManを持っています。彼らはストリーミングシステムに登録されています。 Womanクラスにはいくつかの属性があり、最も重要なのはManクラスのインスタンスです。 TMemoryStreamクラスとTStringStreamクラスを使用して、私はTumemoryStreamクラスのWriteComponentメソッドとReadComponentメソッドによって、WomanのMan *、Man *のすべての属性を取得できました。実際にはコンパイラは例外をスローし、その理由はMan *がNULLで正しくロードされていないためです。私のプログラムでは、単純なデータ型や他のクラスのインスタンスを含むすべての属性をロードする必要があります。 Man *がNULLではないように、Womanオブジェクトを正しく読み込む方法を教えてください。ここに私のコードスニペットがあります。TMemoryStreamとTStringStreamを使ってオブジェクトの内部オブジェクトを正しく取得する方法
#include <vcl.h>
#pragma hdrstop
#include <tchar.h>
#include <memory>
#include <iostream>
#include <conio.h>
#include <string>
#pragma argsused
using namespace std;
class Man : public TComponent
{
private:
double fMoney;
public:
__fastcall Man(TComponent* _Owner,double InMoney)
: TComponent(_Owner)
{
fMoney = InMoney;
}
__published:
__property double Money = {read=fMoney, write=fMoney};
};
class Woman : public TComponent
{
private:
int fAge;
UnicodeString fMyName;
Man* fManInClass;
public:
__fastcall Woman(TComponent* _Owner, int InAge, UnicodeString InName)
: TComponent(_Owner)
{
fAge = InAge;
fMyName = InName;
fManInClass = new Man(this, 0);
}
__published:
__property int Age = {read=fAge, write=fAge};
__property UnicodeString MyName = {read=fMyName, write=fMyName};
__property Man* ManInClass = {read = fManInClass, write = fManInClass};
};
void RegisterClassesWithStreamingSystem(void)
{
#pragma startup RegisterClassesWithStreamingSystem
Classes::RegisterClass(__classid(Man));
Classes::RegisterClass(__classid(Woman));
}
int _tmain(int argc, _TCHAR* argv[])
{
Woman* FirstWoman = new Woman(NULL, 25, "Anjelina");
FirstWoman->ManInClass->Money = 2000;
UnicodeString as;
auto_ptr<TMemoryStream> MStr(new TMemoryStream);
auto_ptr<TStringStream> SStr(new TStringStream(as));
MStr->WriteComponent(FirstWoman);
MStr->Seek(0, soFromBeginning);
ObjectBinaryToText(MStr.get(), SStr.get());
SStr->Seek(0, soFromBeginning);
as = SStr->DataString;
auto_ptr<TMemoryStream> pms(new TMemoryStream);
auto_ptr<TStringStream> pss(new TStringStream(as));
TComponent *pc;
ObjectTextToBinary(pss.get(), pms.get());
pms->Seek(0, soFromBeginning);
pc = pms->ReadComponent(NULL);
Woman* AWoman = dynamic_cast<Woman*>(pc);
cout << AWoman->Age << endl;
cout << AWoman->MyName.c_str() << endl;
cout << AWoman->ManInClass->Money << endl; // AWoman->ManInClass is NULL -> Exception
delete FirstWoman;
pc->Free();
getch();
return 0;
}
「TMemoryStream」と「TStringStream」とは何ですか?これらは標準的なC++クラスではありません。実際に特定のフレームワークを使用していますか? –
はい。 Embarcadero XE1でC++ビルダーとコーディングを使用しています –
少なくとも、[tag:C++ builder]のタグがあります。それ以外の場合、あなたの質問はあまりにも広範です。その特定のフレームワークについてはわかりません(少なくとも、C++コミュニティの大きな部分ではない)。 –