のオブジェクト上のベースクラスのメンバ関数へのポインタを使用すると、コードスニペットを次の検討:派生クラス
struct Base {
std::string foo() const { std::cout << "Base::foo()\n"; return "X"; }
};
struct Der : Base {};
struct Ptr : std::integral_constant<std::string (Base:: *)() const, &Base::foo> {};
および使用のようである:所望のよう
Der der;
std::string s = (der.*Ptr::value)();
std::cout << s << "\n";
出力は:
をBase::foo()
X
すべてうまくいくようです。
私の質問はこのコードは正しいですか?あるいは、私は未定義の行動の土地を歩いていますか?派生クラスのオブジェクトの基底クラスのメンバ関数へのポインタを直接使用できますか?
前に 'std :: integral_constant'にポインタを格納することを考えていませんでした。それは面白いアプローチです。 –
@FrançoisAndrieux私の質問のその部分が他の人に役立つことを聞いてよかった:) –