起こったことは次のとおりです。
Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );
Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );
Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );
Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );
Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );
Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );
Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );
Copy Source | Copy HTML TUser user; user.StoreValue(); double SalaryRef; user.SetField( "Salary" , SalaryRef); // user.GetField( "Salary" , SalaryRef); // - // , - double Salary = user.GetField< double >( "Salary" );
完全な秘密は、「魔法の泡」にあります-TUserが継承されるクラスと、1つの関数-StoreValue()。
カットの下の詳細。
重要:このソリューションはクロスプラットフォームであり、STLとテンプレートを「認識する」C ++コンパイラのみが必要です。
最初は-同義語を入力します-そのため、実験の場合-変更が容易になりました。
Copy Source | Copy HTML
- typedef std :: string TWString;
- typedef __int64 bigint;
- const double DBL_NULL = 0 ;
- const bigint INT_NULL = -1 ;
- const TWString STR_NULL = TWString();
「グッズ」を提供する基本クラス
Copy Source | Copy HTML
- struct BaseType
- {
- bool IsStored;
- 列挙型 BaseValueState {bvsStore、bvsSet、bvsGet、bvsErase};
- BaseType ():IsStored( false )
- {
- }
- virtual〜BaseType ()
- {
- if (IsStored)
- {
- int tempInt = int ();
- double tempDbl = double ();
- TWString tempStr = TWString ();
- FieldStorage( "" 、tempInt、bvsErase);
- FieldStorage( "" 、tempStr、bvsErase);
- FieldStorage( "" 、tempDbl、bvsErase);
- }
- }
- 仮想ボイド StoreValue()= 0 ;
- テンプレート< クラス T >
- void FieldStorage( const TWString &Tag、 T &Value、
- BaseValueState State = bvsStore)
- {
- typedef std :: map < void *、std :: map < TWString 、 T * >> FieldMap;
- 静的 FieldMap値。
- 静的 T Empty = T ();
- if (!IsStored)
- IsStored = true ;
- スイッチ (状態)
- {
- ケース bvsStore:
- 値[ this ] [Tag] =&Value;
- 休憩 ;
- ケース bvsSet:
- {
- T * Ptr =値[ this ] [タグ];
- if (Ptr)
- * Ptr =値;
- }
- 休憩 ;
- ケース bvsGet:
- {
- T * Ptr =値[ this ] [タグ];
- 値=(Ptr)? * Ptr:空;
- }
- 休憩 ;
- ケース bvsErase:
- Values.erase( this );
- 休憩 ;
- }
- }
- テンプレート<typename T >
- void GetField( const TWString &Tag、 T &Value)
- {
- FieldStorage(タグ、値、bvsGet);
- }
- テンプレート<typename T >
- void SetField ( const TWString &Tag、 T &Value)
- {
- FieldStorage(タグ、値、bvsSet);
- }
- テンプレート<typename T >
- T GetField( const TWString &Tag)
- {
- T値
- FieldStorage(タグ、値、bvsGet);
- 戻り値;
- }
- };
そして、ここで彼から継承します。
Copy Source | Copy HTML
- struct TUser : パブリック BaseType
- {
- TUser ( int AId = INT_NULL、 const TWString&ALogin = STR_NULL、 const double ASalary = DBL_NULL)
- :ID(AId)、ログイン(ALogin)、給与(ASalary)
- {}
- bigint id;
- TWStringログイン。
- 二重給与。
- void StoreValue()
- {
- FieldStorage( "Id" 、Id);
- FieldStorage( "ログイン" 、ログイン);
- FieldStorage( "Salary" 、Salary);
- }
- };