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

Access control and nested classes

P: n/a
Hi!
I'm learning C++ from the book "Thinking in C++". Now I'm reading about
nested classes and access control. I have written this code

#include <iostream>

class Outer
{
private:
int outer_data;

public:
class Inner
{
private:
int inner_data;

public:
friend struct Outer;
Inner(int data) { inner_data = data; };
int inner_func(Outer *o) { return o->outer_data; };
};

Outer(int data) { outer_data = data; };
int outer_func(Inner *i) { return i->inner_data; };
};

int main()
{
Outer o(5);
Outer::Inner i(2);

std::cout << "Inner data: " << o.outer_func(&i) << std::endl;
std::cout << "Outer data: " << i.inner_func(&o) << std::endl;

return 0;
}

Without using the friend feature, outer_func(Inner *) can't access
inner_data. Why can inner_func(Outer *) access outer_data without the
friend declaration in Outer? The example I have found in the book (page
266) gives friend status to the inner class in the outer class (I think that
the book solution is correct).

I'm using gcc 3.3.5 and the above example compiles without errors.

Another question: where is the correct place to declare a friend class? In
the private or public section of the class?

Thanks in advance for any information,

Fabio
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Fabio Rossi wrote:
I'm learning C++ from the book "Thinking in C++". Now I'm reading about
nested classes and access control. I have written this code

#include <iostream>

class Outer
{
private:
int outer_data;

public:
class Inner
{
private:
int inner_data;

public:
friend struct Outer;
Inner(int data) { inner_data = data; };
int inner_func(Outer *o) { return o->outer_data; };
};

Outer(int data) { outer_data = data; };
int outer_func(Inner *i) { return i->inner_data; };
};

int main()
{
Outer o(5);
Outer::Inner i(2);

std::cout << "Inner data: " << o.outer_func(&i) << std::endl;
std::cout << "Outer data: " << i.inner_func(&o) << std::endl;

return 0;
}

Without using the friend feature, outer_func(Inner *) can't access
inner_data. Why can inner_func(Outer *) access outer_data without the
friend declaration in Outer?
'Inner' is a member. Members by design have access to all other members
of the same class.
The example I have found in the book (page
266) gives friend status to the inner class in the outer class (I think that
the book solution is correct).
There is a proposal on the table to straighten out those issues. I don't
remember the number, you can look it up on the official standard committee
site (http://www.open-std.org/jtc1/sc22/wg21/).

I'm using gcc 3.3.5 and the above example compiles without errors.

Another question: where is the correct place to declare a friend class? In
the private or public section of the class?


It does not matter.

V
Jul 23 '05 #2

P: n/a
Victor Bazarov wrote:
Without using the friend feature, outer_func(Inner *) can't access
inner_data. Why can inner_func(Outer *) access outer_data without the
friend declaration in Outer?
'Inner' is a member. Members by design have access to all other members
of the same class.


But isn't 'Inner' just defined inside the scope of 'Outer'? In the code
there is not an instance of 'Inner' for any instance of 'Outer', so 'Inner'
isn't a real member, isn't true?
There is a proposal on the table to straighten out those issues. I don't
remember the number, you can look it up on the official standard committee
site (http://www.open-std.org/jtc1/sc22/wg21/).


I'll look there but I'm a little inexpert :-)

Fabio
Jul 23 '05 #3

P: n/a
Fabio Rossi wrote:
Victor Bazarov wrote:

Without using the friend feature, outer_func(Inner *) can't access
inner_data. Why can inner_func(Outer *) access outer_data without the
friend declaration in Outer?
'Inner' is a member. Members by design have access to all other members
of the same class.

But isn't 'Inner' just defined inside the scope of 'Outer'?


Yes. That's the definition of "a member".
In the code
there is not an instance of 'Inner' for any instance of 'Outer', so 'Inner'
isn't a real member, isn't true?


The _type_ 'Inner' is a member of 'Outer'. I guess you need to amend your
understanding of "membership".

V
Jul 23 '05 #4

P: n/a
Victor Bazarov wrote:
But isn't 'Inner' just defined inside the scope of 'Outer'?


Yes. That's the definition of "a member".
In the code
there is not an instance of 'Inner' for any instance of 'Outer', so
'Inner' isn't a real member, isn't true?


The _type_ 'Inner' is a member of 'Outer'. I guess you need to amend your
understanding of "membership".


After this discussion the concept is clearer. Thanks!

Fabio
Jul 23 '05 #5

P: n/a
Victor Bazarov wrote:
There is a proposal on the table to straighten out those issues. I don't
remember the number, you can look it up on the official standard
committee site (http://www.open-std.org/jtc1/sc22/wg21/).


I have found this:

http://www.open-std.org/JTC1/SC22/WG...efects.html#45

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.