Forward declaring a private struct

Hello forum,
I have a question on gaining access to a private struct via forward declaration. I have a class such as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class foo
{
  public:
   ...
   bar::iterator begin() {return my_bar.begin();}
   bar::iterator end() {return my_bar.end();}

  private:
    ...
    struct bar
    {
      typedef std::vector<float>::iterator iterator;
      iterator begin(){return my_data.begin();}
      iterator end(){return my_data.end();}

      private:
        std::vector<float> my_data;
    };
    bar my_bar;
}

the code wont compile because in the public section it does not yet know about the struct bar. The code works if I just flip the definitions such that the private: section comes before the public: section. But, I was wondering if there is another way to get around this so that I can keep the organization public:, protected:, private:? Thanks!
Last edited on
No. It follows nomal rules where entity should be defined before use.
Ok thank, yeah I tried a few things and couldn't get it to work. I have another similar question. Can I forward declare bar and implement it outside of the class foo? I have tried and it also does not seem to work...something along these lines

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class foo
{
  private:
    ...
    struct bar; //Forward declare bar???
    bar my_bar;

  public:
   ...
   bar::iterator begin() {return my_bar.begin();}
   bar::iterator end() {return my_bar.end();}
}

struct foo::bar
{
  typedef std::vector<float>::iterator iterator;
  iterator begin(){return my_data.begin();}
  iterator end(){return my_data.end();}

  private:
    std::vector<float> my_data;
};
you can forward declare bar, but you can use it only as other forward declaration: declaring and moving around pointers and references to that class. To use nested types and member functions, you need to define class itself before use.
Topic archived. No new replies allowed.