Accessing members of nested classes

Discussion in 'C++' started by jblazi, Nov 8, 2003.

  1. jblazi

    jblazi Guest

    Let us assume, I have to classes A and B:

    class A {
    "type" x;
    };

    and

    class B {
    A a[30];
    };

    Now if x and a are private and I want to access x having an instance of B,
    I have to write something like

    B.get_A(i).get_x()

    where the member functions "get_A(int i)" in B and "get_x()" in A are to
    be defined.

    Is there a possibility that is less clumsy?

    TIA,

    jb




    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    jblazi, Nov 8, 2003
    #1
    1. Advertising

  2. jblazi escribió:

    > Let us assume, I have to classes A and B:
    >
    > class A {
    > "type" x;
    > };
    >
    > and
    >
    > class B {
    > A a[30];
    > };
    >
    > Now if x and a are private and I want to access x having an instance ofB,
    > I have to write something like
    >
    > B.get_A(i).get_x()
    >
    > where the member functions "get_A(int i)" in B and "get_x()" in A are to
    > be defined.
    >
    > Is there a possibility that is less clumsy?


    B.do_something (i);

    Regards.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Nov 8, 2003
    #2
    1. Advertising

  3. jblazi

    Moonlit Guest

    Hi,

    "jblazi" <> wrote in message
    news:p...
    > Let us assume, I have to classes A and B:
    >
    > class A {
    > "type" x;
    > };
    >
    > and
    >
    > class B {
    > A a[30];
    > };
    >
    > Now if x and a are private and I want to access x having an instance of B,
    > I have to write something like
    >
    > B.get_A(i).get_x()


    Personally I would add a function to B that would retrieve the value from A
    ( so the fact that another class is contained in A is hidden).

    class B {
    const "type"& get_x( int Subscript) const { return a[
    Subscript ].get_x(); }
    }

    However this of course requires more work.


    Regards, Ron AF Greve.

    >
    > where the member functions "get_A(int i)" in B and "get_x()" in A are to
    > be defined.
    >
    > Is there a possibility that is less clumsy?
    >
    > TIA,
    >
    > jb
    >
    >
    >
    >
    > ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet

    News==----
    > http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000

    Newsgroups
    > ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption

    =---
    Moonlit, Nov 8, 2003
    #3
  4. jblazi

    Moonlit Guest

    Oops, meant "another class is contained in B is hidden"

    Regards, Ron AF Greve.
    Moonlit, Nov 8, 2003
    #4
  5. "jblazi" <> wrote in message
    news:p...
    | Let us assume, I have to classes A and B:
    |
    | class A {
    | "type" x;
    | };
    |
    | and
    |
    | class B {
    | A a[30];
    | };
    |
    | Now if x and a are private and I want to access x having an instance of B,
    | I have to write something like
    |
    | B.get_A(i).get_x()
    |
    | where the member functions "get_A(int i)" in B and "get_x()" in A are to
    | be defined.
    |
    | Is there a possibility that is less clumsy?

    Without any understanding of what your class is actually
    going to do, it is difficult to say which is the best way,
    because there are many ways to do it.

    Here is *one* example, that uses 'operator int()'.

    A 'friend' would be another, and so on with a 'getter()'.

    It all depends on what it will do, and how secure you want
    it to be:

    class A
    {
    private:
    int X;
    public:
    operator int() const { return X; }
    };

    class B
    {
    private:
    A a[ 30 ];
    public:
    int GetA( const int& i ) const { return a[ i ]; }
    };

    int main()
    {
    B b;
    int N = b.GetA( 10 );

    return 0;
    }

    Cheers.
    Chris Val
    Chris \( Val \), Nov 8, 2003
    #5
    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. CoolPint
    Replies:
    8
    Views:
    953
    Jeff Schwab
    Dec 14, 2003
  2. Replies:
    1
    Views:
    302
    Victor Bazarov
    Sep 6, 2005
  3. Mr Dyl
    Replies:
    2
    Views:
    469
    Mr Dyl
    Dec 1, 2005
  4. Daz
    Replies:
    5
    Views:
    567
  5. Rit
    Replies:
    23
    Views:
    917
    Mike Schilling
    Jan 3, 2010
Loading...

Share This Page