3

次のような状況になりました。

struct Foo
{
    void Barry() { }
};

struct Bar : private Foo
{
    template <class F> void Bleh(F Func) { Func(); }
};

struct Fooey : public Bar
{
    void Blah() { Foo f; Bar::Bleh(std::bind(&Foo::Barry, &f)); }
};

そして、コンパイルされません (g++ 4.7.3)。エラーあり:

test.cpp: In member function ‘void Fooey::Blah()’:
test.cpp:4:1: error: ‘struct Foo Foo::Foo’ is inaccessible
test.cpp:15:23: error: within this context
test.cpp:4:1: error: ‘struct Foo Foo::Foo’ is inaccessible
test.cpp:15:47: error: within this context

ただし、これを行うと:

class Fooey;
void DoStuff(Fooey* pThis);

struct Fooey : public Bar
{
    void Blah() { DoStuff(this); }
};

void DoStuff(Fooey* pThis)
{
    Foo f;
    pThis->Bleh(std::bind(&Foo::Barry, &f));
}

それはうまくコンパイルされます。この背後にあるロジックは何ですか?

4

4 に答える 4

1

問題は、内部Fooまたはそれから派生したクラスFoo注入されたクラス名であることです。内にスコープが設定された名前。これにより、外側Fooの名前空間内のクラスの同じ名前が隠されます。この場合、private 継承のためにアクセスできません。

名前空間の名前を明示的に参照することで、この問題を回避できます。この場合は::Foo. 残念ながら、クラスを別の名前空間に移動すると、それが壊れます。

于 2013-08-22T18:05:19.397 に答える