how to access the outer structure member variable from inner struture?

Discussion in 'C++' started by dobest03, May 4, 2006.

  1. dobest03

    dobest03 Guest

    Hi.

    Are there any way to access the integer member 'a' of outer structure
    from
    inner structure's member function func_inner()?
    See below the structure...

    Thanks.

    struct outer {
    int a;
    struct inner {
    int b;
    void func_inner(void)
    {
    cout << "Inner func" << endl;
    cout << "inner b: " << b << endl;
    //cout << "outer a: " << outer::a << endl;
    //cout << "outer a: " << a << endl;
    }
    } i;
    void func_outer(void)
    {
    cout << "Outer func" << endl;
    cout << "inner b: " << i.b << endl;
    cout << "outer a: " << a << endl;
    }
    };
     
    dobest03, May 4, 2006
    #1
    1. Advertising

  2. "dobest03" <> schrieb im Newsbeitrag
    news:...
    > Hi.
    >
    > Are there any way to access the integer member 'a' of outer
    > structure
    > from
    > inner structure's member function func_inner()?
    > See below the structure...
    >
    > Thanks.
    >
    > struct outer {
    > int a;


    // make inner a friend of outer
    struct inner;
    friend class inner;

    > struct inner {

    // have a poitner to outer
    outer& rOuter;
    // no default c'tor: have to fill reference
    inner(outer& out) {rOuter&out;}


    > int b;
    > void func_inner(void)
    > {
    > cout << "Inner func" << endl;
    > cout << "inner b: " << b << endl;
    > cout << "outer a: " << rOuter.a << endl;
    > }
    > } i;
    > void func_outer(void)
    > {
    > cout << "Outer func" << endl;
    > cout << "inner b: " << i.b << endl;
    > cout << "outer a: " << a << endl;
    > }


    // constructor must initialize 'i'
    outer(): i(this) {}
    > };



    HTH,
    -Gernot
     
    Gernot Frisch, May 4, 2006
    #2
    1. Advertising

  3. dobest03

    Mark P Guest

    Re: how to access the outer structure member variable from innerstruture?

    Gernot Frisch wrote:
    > "dobest03" <> schrieb im Newsbeitrag
    > news:...
    >> Hi.
    >>
    >> Are there any way to access the integer member 'a' of outer
    >> structure
    >> from
    >> inner structure's member function func_inner()?
    >> See below the structure...
    >>
    >> Thanks.
    >>
    >> struct outer {
    >> int a;

    >
    > // make inner a friend of outer
    > struct inner;
    > friend class inner;


    There's really no need for this since the default access specifier for
    members of a struct is public.

    >
    >> struct inner {

    > // have a poitner to outer
    > outer& rOuter;
    > // no default c'tor: have to fill reference
    > inner(outer& out) {rOuter&out;}


    This is the right idea but a reference has to be seated in the ctor
    initializer list, not in the ctor body.

    inner (outer& out) : rOuter(out) {}

    In any event, to state explicitly what your solution implies: an
    instance of a nested class is not automatically associated with an
    instance of the outer class-- indeed one can instantiate the inner class
    without ever instantiating the outer class. (If memory serves, this is
    in contrast to non-static Java inner classes which are always associated
    with an instance of the outer class.)

    >
    >
    >> int b;
    >> void func_inner(void)
    >> {
    >> cout << "Inner func" << endl;
    >> cout << "inner b: " << b << endl;
    >> cout << "outer a: " << rOuter.a << endl;
    >> }
    >> } i;
    >> void func_outer(void)
    >> {
    >> cout << "Outer func" << endl;
    >> cout << "inner b: " << i.b << endl;
    >> cout << "outer a: " << a << endl;
    >> }

    >
    > // constructor must initialize 'i'
    > outer(): i(this) {}


    Almost... should actually be:
    outer() : i(*this) {}

    >> };

    >
    >


    -Mark
     
    Mark P, May 4, 2006
    #3
    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. lonelyplanet999

    Access outer / inner class variables query

    lonelyplanet999, Nov 11, 2003, in forum: Java
    Replies:
    3
    Views:
    5,312
    lonelyplanet999
    Nov 18, 2003
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,232
    VisionSet
    Nov 13, 2003
  3. Newbie
    Replies:
    4
    Views:
    1,402
    Newbie
    Aug 23, 2006
  4. Zeba

    Directory struture

    Zeba, Mar 23, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    334
    marss
    Mar 23, 2007
  5. Replies:
    2
    Views:
    242
Loading...

Share This Page