本文整理汇总了C++中Component::Add方法的典型用法代码示例。如果您正苦于以下问题:C++ Component::Add方法的具体用法?C++ Component::Add怎么用?C++ Component::Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Component
的用法示例。
在下文中一共展示了Component::Add方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
// 创建一个树形结构
// 创建根节点
Component *pRoot = new Composite("江湖公司(任我行)");
// 创建分支
Component *pDepart1 = new Composite("日月神教(东方不败)");
pDepart1->Add(new Leaf("光明左使(向问天)"));
pDepart1->Add(new Leaf("光明右使(曲洋)"));
pRoot->Add(pDepart1);
Component *pDepart2 = new Composite("五岳剑派(左冷蝉)");
pDepart2->Add(new Leaf("嵩山(左冷蝉)"));
pDepart2->Add(new Leaf("衡山(莫大)"));
pDepart2->Add(new Leaf("华山(岳不群)"));
pDepart2->Add(new Leaf("泰山(天门道长)"));
pDepart2->Add(new Leaf("恒山(定闲师太)"));
pRoot->Add(pDepart2);
// 添加和删除叶子
pRoot->Add(new Leaf("少林(方证大师)"));
pRoot->Add(new Leaf("武当(冲虚道长)"));
Component *pLeaf = new Leaf("青城(余沧海)");
pRoot->Add(pLeaf);
// 小丑,直接裁掉
pRoot->Remove(pLeaf);
// 递归地显示组织架构
pRoot->Operation(1);
// 删除分配的内存
SAFE_DELETE(pRoot);
getchar();
return 0;
}
示例2: main
int main()
{
Component* cmp;
Leaf* lfp;
Leaf lf;
Composite cs;
cmp = &lf;
cmp->Add(cmp);
cmp->Remove(cmp);
lfp = &lf;
lfp->Add(cmp); //compile error
lf.Add(cmp); //compile error
return 0;
}
示例3: main
void main()
{
try
{
Component * HeadComponent;
Component * CurrComponent;
Component * SubComponent;
Component * SubSubComponent;
ComponentIterator * CompIter;
int x;
int y;
int z;
char name[100] = {"Object level"};
char actual_name[100];
char add_string[100];
// build it up.....
HeadComponent = new Composite("Head");
CurrComponent = HeadComponent;
for (x = 0; x<10 ;x++)
{
strcpy(actual_name,name);
sprintf(add_string,"1-%d",x);
strcat(actual_name,add_string);
if(x == 2 || x == 4)
{
strcpy(actual_name,name);
sprintf(add_string,"1-%d-%d",x,y);
strcat(actual_name,add_string);
SubComponent = new Composite(actual_name);
CurrComponent->Add(SubComponent);
for (y = 0; y < 10; y++)
{
if (y == 7 || y == 9)
{
SubSubComponent = new Composite(actual_name);
SubComponent->Add(SubSubComponent);
}
else
{
SubComponent->Add(new Component(actual_name));
}
}
}
else
{
CurrComponent->Add(new Component(actual_name));
}
}
HeadComponent->Operation();
// test iterator operation!
if (HeadComponent->GetChild())
{
CompIter = HeadComponent->CreateIterator();
while (! CompIter->IsDone())
{
CompIter->CurrentItem()->PrintName();
(*CompIter)++;
}
}
delete CompIter;
delete HeadComponent;
}
catch(EXCEPTION_CODE ec)
{
switch (ec)
{
case EXC_MEMORY_ALLOC_FAILURE_LISTCLASS :
cout << "Exception:" << "mem alloc failure.. list class" << endl;
break;
case EXC_HEAP_ALLOC_FAILURE :
cout << "Exception:" << "Heap allocation failure!" << endl;
break;
case EXC_BAD_ADD_COMPONENT :
cout << "Exception:" << "Tried to add w/ component object" << endl;
break;
case EXC_BAD_REMOVE_COMPONENT :
cout << "Exception:" << "Tried to remove w/ component object" << endl;
break;
case EXC_BAD_COUNT_CALL :
//.........这里部分代码省略.........