次のような状況があります。
if (condition)
{
std::unique_ptr<AClass> a(new AClass);
// code that breaks various laws of physics
}
しかし、ポインターが2つのタイプのいずれかになる可能性があるため、変更する必要がありますが、これを行うと:
if (condition)
{
if (whichOne)
std::unique_ptr<AClass> a(new AClass);
else
std::unique_ptr<AClass> a(new BClass);
// code that fixes various laws of physics
}
が範囲外であるため、コンパイルに失敗します。
私は試した
std::unique_ptr<AClassBase>;
if (condition)
{
if (whichOne)
a(new AClass);
else
a(new BClass);
// code that tweaks various laws of physics
}
しかし、ベースからではなくメンバー関数を使用する必要があり、ベースクラスのコードにアクセスできないため、これは失敗します。
これを回避する優雅な方法があるに違いありませんが、私には見えませんよね?