accessing the member of parent class

Discussion in 'C++' started by free2cric@yahoo.com, Oct 5, 2006.

  1. Guest

    Hi I have a simple problem

    class aaaa
    {
    public:
    int a;
    public:
    int getParentVar()
    }

    class bbb
    {
    public:
    int b;
    aaaa google;
    }

    int aaaa::getParentVar()
    {
    /* access class bbb's variable that is int b
    }

    so please tell me how to get int b of class bbb in the method
    getParentVar of class aaa.

    Thanks,
    Vijay
    , Oct 5, 2006
    #1
    1. Advertising

  2. wrote:

    > class aaaa
    > {
    > public:
    > int a;
    > public:
    > int getParentVar()
    > }
    >
    > class bbb
    > {
    > public:
    > int b;
    > aaaa google;
    > }
    >
    > int aaaa::getParentVar()
    > {
    > /* access class bbb's variable that is int b
    > }
    >
    > so please tell me how to get int b of class bbb in the method
    > getParentVar of class aaa.


    you can't there is no relationship between aaaa and bbb.
    Also note bbb is *not* a "parent" of aaaa

    --
    Nick Keighley
    Nick Keighley, Oct 5, 2006
    #2
    1. Advertising

  3. SirMike Guest

    Dnia 5 Oct 2006 01:21:46 -0700, napisa³(a):

    > so please tell me how to get int b of class bbb in the method
    > getParentVar of class aaa.


    There is no possibility to do so. Hovewer, you can have a pointer to the
    class bbbb in the class aaaa and initialize it in a constructor.

    --
    SirMike - http://www.sirmike.org

    C makes it easy to shoot yourself in the foot; C++ makes it harder, but
    when you do, it blows away your whole leg. - Bjarne Stroustrup
    SirMike, Oct 5, 2006
    #3
  4. Jim Langston Guest

    <> wrote in message
    news:...
    > Hi I have a simple problem
    >
    > class aaaa
    > {
    > public:
    > int a;
    > public:
    > int getParentVar()
    > }
    >
    > class bbb
    > {
    > public:
    > int b;
    > aaaa google;
    > }
    >
    > int aaaa::getParentVar()
    > {
    > /* access class bbb's variable that is int b
    > }
    >
    > so please tell me how to get int b of class bbb in the method
    > getParentVar of class aaa.
    >
    > Thanks,
    > Vijay


    aaaa and bbb are unrelated in any way, other than that bbb contains an
    instance of aaaa.

    However, the way I handle this is by passing to aaaa's constructor a pointer
    to bbb as the parent and initialize it in the initialization list (giving me
    a warning about passing self in constructor, but that's fine in this case).

    class bbb;
    class aaaa
    {
    public:
    aaaa( bbb* parent ): Parent( parent );
    int a;
    int getParentVar() const;
    private:
    bbb* Parent;
    };

    class bbb
    {
    public:
    bbb(): google( self ) {};
    int b;
    aaa google;
    };

    int aaaa::getParentVar()
    {
    return Parent->b;
    }
    Jim Langston, Oct 7, 2006
    #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. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  2. Steven T. Hatton
    Replies:
    2
    Views:
    413
    tom_usenet
    Aug 16, 2004
  3. Siemel Naran
    Replies:
    4
    Views:
    801
    Micah Cowan
    Jan 12, 2005
  4. cy
    Replies:
    2
    Views:
    719
    Piotr Kobzda
    Apr 13, 2007
  5. metal
    Replies:
    8
    Views:
    501
    Bruno Desthuilliers
    Oct 30, 2009
Loading...

Share This Page