Accessing members of nested classes

J

jblazi

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
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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.
 
M

Moonlit

Hi,

jblazi said:
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
=---
 
C

Chris \( Val \)

| 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top