473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Accessing members of nested classes

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
4 1828
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
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
Oops, meant "another class is contained in B is hidden"

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
9
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here?...
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
6
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.