私は最近、このようなコードをリファクタリングしました(MyClass
へMyClassR
)。
#include <iostream>
class SomeMember
{
public:
double m_value;
SomeMember() : m_value(0) {}
SomeMember(int a) : m_value(a) {}
SomeMember(int a, int b)
: m_value(static_cast<double>(a) / 3.14159 +
static_cast<double>(b) / 2.71828)
{}
};
class MyClass
{
public:
SomeMember m_first, m_second, m_third;
MyClass(const bool isUp, const int x, const int y)
{
if (isUp)
{
m_first = SomeMember(x);
m_second = SomeMember(y);
m_third = SomeMember(x, y);
}
else
{
m_first = SomeMember(y);
m_second = SomeMember(x);
m_third = SomeMember(y, x);
}
}
};
class MyClassR
{
public:
SomeMember m_first, m_second, m_third;
MyClassR(const bool isUp, const int x, const int y)
: m_first(isUp ? x : y)
, m_second(isUp ? y : x)
, m_third(isUp ? x, y : y, x)
{
}
};
int main()
{
MyClass a(true, 1, 2);
MyClassR b(true, 1, 2);
using namespace std;
cout.precision(10);
cout
<< "a:" << endl
<< "\tfirst: " << a.m_first.m_value
<< "\tsecond: " << a.m_second.m_value
<< "\tthird: " << a.m_third.m_value << endl;
cout
<< "b:" << endl
<< "\tfirst: " << b.m_first.m_value
<< "\tsecond: " << b.m_second.m_value
<< "\tthird: " << b.m_third.m_value << endl;
return 0;
}
- エラーは何ですか、
- なぜコンパイルするのか (VC6および VC9 警告レベル 4 でテスト済み: 苦情なし)
- それを行う正しい方法は何ですか?
私は(仮定して)すでにこれらすべての答えを持っていますが、共有するのは興味深い問題だと思います。
拡張コードを更新
して、「コピー & ペースト & 実行」できるようにします。VC9 でも不満はなかったので、ここでは VC6 は問題ではありません。
完全を期すために、出力は次のとおりです。
a:
first: 1 second: 2 third: 1.054069532
b:
first: 1 second: 2 third: 1.004499999