我在基类构造函数中有一个逻辑。逻辑结果必须在派生类构造函数中的临时变量中捕获。有办法做到吗?
例如
class Base
{
Base() { int temp_value = some_logic; }
};
class Derived : public Base
{
Derived() { // need the temp value here.. }
};
谢谢, 戈库尔。
最佳答案
我想我能想到的最简单的方法是将 some_logic 分离到它自己的方法中......
class Base
{
Base() { int temp_value = initializationLogic(); }
int initializationLogic(){ return some-logic;}
};
class Derived : public Base
{
Derived() { int temp_value_here_too = initializationLogic(); }
};
关于基类构造函数中的 C++ 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3850408/