Mis*_*vić 3 c++ gcc compiler-errors class
我有这样的情况:
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)); }
};
Run Code Online (Sandbox Code Playgroud)
它不编译(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
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做:
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));
}
Run Code Online (Sandbox Code Playgroud)
它编译得很好.这背后的逻辑是什么?
这里
struct Fooey : public Bar
{
void Blah() { Foo f; Bar::Bleh(std::bind(&Foo::Barry, &f)); }
};
Run Code Online (Sandbox Code Playgroud)
name lookup for Foo查找Bar由于Bar私有继承而无法访问的基类.
要修复它,请完全限定名称:
void Blah() { ::Foo f; Bar::Bleh(std::bind(&::Foo::Barry, &f)); }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |