我知道在 C++11 构造函数委托可以是这样的:
class Foo
{
public:
Foo()
{
// Some code to be ran first
}
Foo(std::string bar): Foo() // Runs the code to be ran first
{
// Some code to be ran second, presumably using bar
}
};
我想知道如何以某种方式扭转这种情况。也就是说,在Foo()
调用构造函数的情况下,我想运行一些代码来找出 a 的值,std::string
然后用于Foo(std::string bar)
完成初始化。所以Foo()
运行它自己的代码和 中的代码Foo(std::string bar)
,而后者只运行它自己的代码,比如
class Foo
{
public:
Foo()
{
std::string bar_figured_out;
// Figures out the value for bar_figured_out
Foo(bar_figured_out); // I know this wouldn't work, just an example of what I'm trying to do.
}
Foo(std::string bar):
{
// Some code to finish class initialization, using bar
}
};
有没有办法使用构造函数委托来完成这个?