nested classからは親のprivate空間にアクセスできるか。

nested classからは親のprivate空間にアクセスできるか。

C++Builder 2009でもエラーになりました。残念。

[BCC32 エラー] File1.cpp(25): E2247 'Hoge::func()' はアクセスできない

元ネタは、!Inner classes have no special access to the outer class in C++.

class Hoge {
public:
  Hoge () : bar_(*this) {}
private:
  class Bar {
  public:
    Bar(Hoge& hoge) : hoge_(hoge) {}
    void func() {
      hoge_.func();
    }
    Hoge& hoge_;
  };
  Bar bar_;
  void func(){
    cout << "func" << endl;
  }
public:
  Bar& Bar() { return bar_; }
};

int main()
{
  Hoge hoge;
  hoge.Bar().func();
  return 0;
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください