struct declaration into another struct

Discussion in 'C++' started by slocum, Apr 11, 2008.

  1. slocum

    slocum Guest

    I have a struct like that

    struct A {
    struct B {
    .....
    }
    ...
    }

    I have to declare previously all that struct and their friends

    struct A;
    friend struct A;

    but how to declare struct B and which is declared into struct A ?

    struct A::B; ?
    friend struct A::B ?

    it doesn't work...
    slocum, Apr 11, 2008
    #1
    1. Advertising

  2. slocum

    Looney Guest

    On Apr 11, 3:06 pm, slocum <> wrote:
    > I have a struct like that
    >
    > struct A {
    > struct B {
    > .....
    > }
    > ...
    >
    > }
    >
    > I have to declare previously all that struct and their friends
    >
    > struct A;
    > friend struct A;
    >
    > but how to declare struct B and which is declared into struct A ?
    >
    > struct A::B; ?
    > friend struct A::B ?
    >
    > it doesn't work...


    You can not just forward declare A::B and make it work
    a nested type must be visible before hand so consider doing the
    following
    class D
    {
    friend struct A;
    };

    struct A
    {
    int m_i;
    struct B
    {
    int m_i;
    };
    };

    class C
    {
    friend struct A;
    friend struct A::B;
    };

    void somefunc()
    {
    D d;
    C c;
    }

    make sure A and B are defined in the translation unit,
    though usually a class type(class, structor union) does
    not need to be declared or defined before a friend declaration
    though if it is a class template (say in my eg if C was class template
    then)
    then A and B must be visible before hand(at least fwd declared).

    It seems like the same is required for nested class types as well.

    Also watch out for the following things are
    Looney, Apr 11, 2008
    #2
    1. Advertising

  3. slocum

    Jim Langston Guest

    slocum wrote:
    > I have a struct like that
    >
    > struct A {
    > struct B {
    > .....
    > }
    > ...
    > }
    >
    > I have to declare previously all that struct and their friends
    >
    > struct A;
    > friend struct A;
    >
    > but how to declare struct B and which is declared into struct A ?
    >
    > struct A::B; ?
    > friend struct A::B ?
    >
    > it doesn't work...


    What doesn't work? The following compiles for me:

    struct A
    {
    friend struct B;
    struct B
    {
    int x;
    };
    int y;
    };

    struct C
    {
    friend struct A::B;
    };

    int main()
    {

    }

    Does this answer your question?


    --
    Jim Langston
    Jim Langston, Apr 11, 2008
    #3
  4. slocum

    slocum Guest

    On 11 Kwi, 08:42, "Jim Langston" <> wrote:
    > slocum wrote:
    > > I have a struct like that

    >
    > > struct A {
    > > struct B {
    > > .....
    > > }
    > > ...
    > > }

    >
    > > I have to declare previously all that struct and their friends

    >
    > > struct A;
    > > friend struct A;

    >
    > > but how to declare struct B and which is declared into struct A ?

    >
    > > struct A::B; ?
    > > friend struct A::B ?

    >
    > > it doesn't work...

    >
    > What doesn't work? The following compiles for me:
    >
    > struct A
    > {
    > friend struct B;
    > struct B
    > {
    > int x;
    > };
    > int y;
    >
    > };
    >
    > struct C
    > {
    > friend struct A::B;
    >
    > };
    >
    > int main()
    > {
    >
    > }
    >
    > Does this answer your question?
    >
    > --
    > Jim Langston
    >


    and what if struct A and B are template ? How to declare a friend for
    nested template struct ?
    template <class> friend struct A::B or what ????
    slocum, Apr 11, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    945
  2. Alex Vinokur
    Replies:
    4
    Views:
    571
    Jonathan Turkanis
    Apr 5, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,345
    Chris Fogelklou
    Apr 20, 2004
  4. Replies:
    18
    Views:
    7,036
    Karl Heinz Buchegger
    Jul 22, 2005
  5. Ehud Shapira
    Replies:
    20
    Views:
    1,099
    Ehud Shapira
    Jun 30, 2007
Loading...

Share This Page