【发布时间】:2011-09-19 23:47:19
【问题描述】:
在我的 FileProc 类中,我有四个函数:
ReadFile(TemplateList<char> &ReadList){}
ReadFile(TemplateListAdv<char> &ReadList){}
ReadFile(CharList &ReadList){}
ReadFile(CharListAdv &ReadList){}
它们都应该调用集中式方法(它们被转换为):
ReadFile(TemplateListEditor<char> &ReadList){} //Contained by FileBasic
关于背景信息,类层次结构如下:
模板列表 -> 字符列表
模板列表 -> 模板列表Adv
CharList -> CharListAdv
模板列表 -> 模板列表编辑器
FileBasic -> FileProc
我的问题是有一个递归函数调用(其中 TemplateList 转换为 TemplateListEditor 将继续调用 TemplateList 函数),尽管类在内部是不同的。类型转换似乎不起作用。如果不重命名函数(这会破坏它应该是通用的),我如何让方法查找正确的方法?
(我很惊讶编译器从未为此标记出歧义解决错误)。
例子:
const bool ReadFile(TL::TemplateList<char> &ReadList, const bool Recursion = false)
{
printf("Is recursion true? %d!\n",Recursion);
TL::TemplateListEditor<char> Temp(ReadList);
//Calls itself instead of
//const bool ReadFile(TL::TemplateListEditor<char> &ReadList, const bool Recursion = false)
if(!ReadFile(static_cast<TL::TemplateListEditor<char> &>(Temp),true ))
{
return false;
}
return true;
}
上面会输出:
递归是真的吗? 0
递归是真的吗? 1
递归是真的吗? 1
等等
令我震惊的是 TemplateListEditor(尽管是静态转换等)不知何故或出于某种令人难以置信的原因,被转换回 TemplateList。编辑器的构造函数都是显式的。
【问题讨论】:
-
你能给出一个实际的代码示例来说明你的意思吗?
-
它是嵌入式的,有点困难。我会尽力。但基本上,编译器以某种方式将 TemplateListEditor 翻译回 TemplateList ......啊哈......复制构造函数不是显式的......我希望它们不是......(对不起,它们是显式的。同样的问题。 )
标签: c++ class methods recursion subclass