Access control and nested classes
From: Fabio Rossi (fabio_at_none.org)
Date: 02/08/05
- Next message: David Komanek: "template class in DLL (g++/Cygwin)"
- Previous message: Michael H Lees: "Run-time type id and Inheritance"
- Next in thread: Victor Bazarov: "Re: Access control and nested classes"
- Reply: Victor Bazarov: "Re: Access control and nested classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 08 Feb 2005 12:55:47 GMT
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
- Next message: David Komanek: "template class in DLL (g++/Cygwin)"
- Previous message: Michael H Lees: "Run-time type id and Inheritance"
- Next in thread: Victor Bazarov: "Re: Access control and nested classes"
- Reply: Victor Bazarov: "Re: Access control and nested classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|