我想实现这样的:现在如何将方法结果作为参数传递给C++中的基类构造函数?
class Base
{
public:
Base(string S)
{
...
};
}
class Derived: Base
{
public:
int foo;
string bar()
{
return stringof(foo); // actually, something more complex
};
Derived(int f) : foo(f), Base(bar())
{
};
}
,这不工作,我想,是因为巴()被调用派生构造函数foo的初始化之前。
我认为添加一个类似bar()的静态函数,它将foo作为参数 - 并在初始化列表中使用它,但我想问问是否还有其他技术可以用来挖掘自己这一个...
编辑:感谢您的反馈 - 这是我将如何处理静态功能。不知道如果静态和非静态函数之间的过载是太聪明了,但是......
class Derived: Base
{
public:
int foo;
static string bar(int f)
{
return stringof(f); // actually, something more complex
}
string bar()
{
return bar(foo);
};
Derived(int f) : Base(bar(f)) , foo(f)
{
};
}
HMM这与平普尔成语组合将是很好 – 2008-12-17 17:36:42
尼斯 - 然而的组合物中的缺点是,以暴露基方法需要“蝙蝠状”存根的数目。 – Roddy 2008-12-17 19:26:43
真的!我想你会因为这个原因而坚持计划A. – 2008-12-17 21:10:45