Access control and nested classes

F

Fabio Rossi

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
 
V

Victor Bazarov

Fabio said:
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
 
F

Fabio Rossi

Victor said:
'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
 
V

Victor Bazarov

Fabio said:
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".

V
 
F

Fabio Rossi

Victor said:
Yes. That's the definition of "a member".


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
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top