我试图覆盖作为参数传递的枚举的默认值,但我得到了默认值。
class MyClass
{
public:
enum MyCalculation
{
SUM,
DIVIDE,
MULTIPLY,
NOCALCULATION
};
void foo(MyCalculation cal = NOCALCULATION);
void DoCalculation();
};
void MyClass::DoCalculation()
{
MyCalculation cal;
// here I am getting the enum value at runtime
int val = // get enum value
switch(val)
{
case 0:
cal = MyClass::SUM;
break;
case 1:
cal = MyClass::DIVIDE;
break;
case 2:
cal = MyClass::MULTIPLY;
break;
}
foo(cal);
}
void MyClass::foo(MyCalculation getCal)
{
cout << getCal;
}
我尝试在DoCalculation方法中通过引用传递,但我总是在foo中获得NOCALCULATION。 我还需要做点什么吗?