By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,170 Members | 1,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,170 IT Pros & Developers. It's quick & easy.

Accessing members of nested classes

P: n/a
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 =---
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
Jul 19 '05 #2

P: n/a
Hi,

"jblazi" <jb****@hotmail.com> wrote in message
news:pa****************************@hotmail.com...
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

=---
Jul 19 '05 #3

P: n/a
Oops, meant "another class is contained in B is hidden"

Regards, Ron AF Greve.
Jul 19 '05 #4

P: n/a

"jblazi" <jb****@hotmail.com> wrote in message
news:pa****************************@hotmail.com...
| 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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.