C++builder 如何将TCollectionItem的2倍派生类添加到TOwnedCollection?

C++builder 如何将TCollectionItem的2倍派生类添加到TOwnedCollection?,c++builder,vcl,tcollectionitem,townedcollection,C++builder,Vcl,Tcollectionitem,Townedcollection,我想使用TOwnedCollection/TCollectionItem实现一个集合或列表。我需要一个具有多态性的类的持久列表(从文件流加载和创建) 以下是我迄今为止的(部分)代码,但我没有成功创建派生类TGenerator,而不是其父TPowerComponent,并将其添加到集合中 //------------------------------------------------------------------------------------- 类别TPC集合:公共城镇集合 { 继

我想使用TOwnedCollection/TCollectionItem实现一个集合或列表。我需要一个具有多态性的类的持久列表(从文件流加载和创建)

以下是我迄今为止的(部分)代码,但我没有成功创建派生类TGenerator,而不是其父TPowerComponent,并将其添加到集合中

//-------------------------------------------------------------------------------------
类别TPC集合:公共城镇集合
{
继承的typedef TOwnedCollection;
私人:
TPowerComponent*uu快速调用GetPowerComponent(int索引);
void uu fastcall SetPowerComponent(int索引,TPowerComponent*值);
公众:
__fastcall TPC集合(TPersistent*所有者);
HIDESBASE TPowerComponent*\uuu快速调用添加(无效);
HIDESBASE TPowerComponent*\uuu快速调用插入(int索引);
__属性TPowerComponent*PCCollection[int Index]={read=GetPowerComponent,write=SetPowerComponent};
};
//-------------------------------------------------------------------------------------
类TPowerComponent:公共TCollectionItem
{
继承的typedef TCollectionItem;
公众:
int X,Y,旋转;
PowSymbType组织型;
__fastcall TPowerComponent(TCollection*Collection,PowSymbType AType);
void uu快速调用分配(TPersistent*源);
虚拟虚空uu快速调用绘制(TCanvas*画布);
};
//-------------------------------------------------------------------------------------
类TGGenerator:公共TPowerComponent
{
typedef TPowerComponent继承;
公众:
双PG、Qgmin、Qgmax、Vsch;
__fastcall TGGenerator(TCollection*Collection,PowSymbType AType);
void uu快速调用分配(TPersistent*源);
虚拟虚空uu快速调用绘制(TCanvas*画布);
};
//-------------------------------------------------------------------------------------
//实施
//-------------------------------------------------------------------------------------
//TPC收集
//-------------------------------------------------------------------------------------
__fastcall TPCCollection::TPCCollection(TPersistent*Owner)
:TOwnedCollection(所有者,uu classid(动力组件))
{
}
//-------------------------------------------------------------------------------------
TPowerComponent*\uuu快速调用TPCCollection::Add()
{
返回静态_cast(继承的::Add());
}
//-------------------------------------------------------------------------------------
TPowerComponent*\uuu快速调用TPCCollection::Insert(int索引)
{
返回静态_cast(继承的::插入(索引));
}
//-------------------------------------------------------------------------------------
TPowerComponent*\uuu快速调用TPCCollection::GetPowerComponent(int索引)
{
返回静态_cast(继承的::GetItem(索引));
}
//-------------------------------------------------------------------------------------
void _fastcall TPCCollection::SetPowerComponent(int索引,TPowerComponent*值)
{
继承::SetItem(索引、值);
}
//-------------------------------------------------------------------------------------
//动力部件
//-------------------------------------------------------------------------------------
__fastcall TPowerComponent::TPowerComponent(TCollection*Collection,PowSymbType AType)
:t集合项目(集合)
{
组型=a型;
旋转=0;
}
//-------------------------------------------------------------------------------------
void\uu fastcall TPowerComponent::Assign(TPersistent*源)
{
TPowerComponent*Src=动态_-cast(源);
if(Src)
{
//从Src复制成员。。。
}
else继承::分配(源);
}
//-------------------------------------------------------------------------------------
//德辛硒
void uu fastcall TPowerComponent::Paint(TCanvas*Canvas)
{
...
}
//-------------------------------------------------------------------------------------
//发电机
//-------------------------------------------------------------------------------------
__快速调用TGenerator::TGenerator(TCollection*Collection,PowSymbType AType)
:t动力组件(集合,AType)
{
PG=0;Qgmin=0;Qgmax=0;Vsch=1.0;Con=-1;
}
//-------------------------------------------------------------------------------------
void\uu fastcall TGenerator::Assign(TPersistent*Source)
{
TGenerator*Src=动态_-cast(源);
if(Src)
{
//从Src复制成员。。。
}
else继承::分配(源);
}
//-------------------------------------------------------------------------------------
//用法
TPC集合*网络=新TPC集合(此);
//用于访问所有集合
对于(int i=0;iCount;i++)
{
((TPowerComponent*)(网络->项目[i])->油漆(帆布);
}
要添加TGGenerator而不是TPowerComponent,我使用:

TGenerator*Gen=新的TGenerator(网络、发电机);
TCollectionItem子项的创建会自动将其自身添加到TCollection中

这里的问题是,我们无法将项目创建过程与将其添加到集合中分开

例如,当我需要另一个可以包含第一个集合列表的某些项的列表时,SelectedComponents可以包含网络集合的一个或一些项,而无需重新创建它们

这可以通过

std::列出所选组件;
但是我不能使用FileStream/persistent list来写/读它们。我需要将它们放在一个t集合中,但不重新创建它们


如何实现?

RTL针对
t收集
对象的本机DFM流仅部分支持多态
t收集项

您可以将多态的
t集合项
对象添加到
t集合中