using
最近、このキーワードの新しい用途を発見しました。機能を参照するのではnamespace
なく、派生クラスの宣言内で。私の場合、これは「operator=」メンバー関数を取り巻く問題に関して適切でした。
宣言を考えると、次のような状況がありました。
class CString{
public:
...//Various functions.
operator=(const CString &cString)
{
//code to share internal array and increment reference count
}
operator=(const wchar_t *pstrString)
{
//code to create a personal internal array,
//resize it appropriately and copy the pstrString argument.
}
...
};
class CStringEx : public CString{
...//various members that expand upon CString's abilities.
};
... のオブジェクトがCStringEx
期待どおりに機能しませんでした:
CStringEx cString;
cString=L"String contents";
代わりに、' CStringEx has no 'operator=()' function that takes an argument of type wchar_t* ' (または - 非常に近い - その効果に近い言葉)というコンパイラ エラーが生成されました。かなりの研究の後、これはoperator=
、派生クラスのコンパイラによって自動的に生成されたメンバー関数でさえ、その親クラスから継承されたものをオーバーライドするためであることがわかりました。これは私には直観に反し、ユーザーフレンドリーに思えます。
ただし、using
キーワードを追加すると:
class CStringEx : public CString{
public:
using CString::operator=;
...
};
...子クラスは親のoperator=
メンバー関数を使用するようになり、すべて問題ありません。
ここまでは順調ですね。ただし、ここや他の場所をさらに読んだ後、多くのプログラマーがusing
この目的での使用を好まないことがわかりました。たとえば、親からすべての operator= を取り込むなど、望ましくない可能性のある副作用について説明しているコメンテーターを読みました。ただし、非常に特殊な状況を除いて、すべての親メンバー関数を継承する理由がわかりません。これが主な懸念事項である場合、誰かがそうすることの一般的な危険性を説明できますか?
私が考えることができる唯一の代替手段は、その親のすべて のメンバー関数の子クラスにスタブ関数を書き出してから、operator=
それらのそれぞれのメンバー関数を明示的に呼び出すことです。
class CStringEx : public CString{
public:
...
const CStringEx& operator=(const wchar_t* pstrString)
{
CString::operator=(pstrString);
return *this;
}
const CStringEx& operator=(const CString &cString)
{
CString::operator=(cString);
return *this;
}
...//and so on...
};
このバージョンと比較すると、using CString::operator=
これは非常に醜く、扱いにくく、面倒です。using
繰り返しになりますが、キーワードを使用しないのはなぜですか?