看一個例子,XE2 C++編譯器還是有改進的,
先上代碼:
#include <vector>class PACKAGE Base{protected : Base(): Port(0){}public : int Port ; virtual bool Init() = 0 ; virtual ~Base() {}};class PACKAGE Base_A :public Base{protected :public : bool Init() {return false;} ; ~Base_A() {} Base_A():Base() {} static Base * CreateMe(){return new Base_A();}};class PACKAGE Base_B :public Base{protected :public : bool Init() {return false;} ; ~Base_B() {} Base_B():Base() {} static Base * CreateMe(){return new Base_B();}};typedef Base *(*TCreateObject)();struct TBaseInfo{ TCreateObject Create ; AnsiString ID ; String Title ;};std::vector<TBaseInfo> All ;void Test(){ TBaseInfo tmp[2] = { {&Base_A::CreateMe,"A","this is A"}, {&Base_B::CreateMe,"A","this is B"}, //XE2有趣的是,這個逗號無論有沒有,均不報錯。 };//現象是XE2能正常賦值,B2007 賦值不正常,B2007 需要TBaseInfo寫一個構造函數//TBaseInfo(TCreateObject,String,String),通過構造函數可以正常賦值。//爲什麽 B2007 不能正常賦值呢? All.resize(2); All[0] = tmp[0]; All[1] = tmp[1];}TForm1 *Form1;//---------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){ Test(); if(All[0].Create == Base_A::CreateMe) Caption = "OK" ; else Caption = "ERROR" ;}//---------------------------------------