473,397 Members | 2,116 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,397 software developers and data experts.

Access control and nested classes

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

Similar topics

7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
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...
9
by: John Harrison | last post by:
Both gcc 3.3.1 and VC++ 7.1 compile the following code. struct Outer { struct Inner { int f() { return c; } }; private: static const int c;
13
by: Will Pittenger | last post by:
I have a Control derived class. When the parent of the control changes the control's Location property, the stack overflows. I have not found a way to find out what was on the stack when it does...
6
by: Marco | last post by:
Howdy! Given: public abstract class A { public abstract int A1(int i); private class B { private int B1(int i) { int j;
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
6
by: moondaddy | last post by:
I'm new to c# and am wondering if its possible to access members of a nested class. Can someone please advise? Thanks. class Program { static void Main(string args) { try { Test1 obj =...
5
by: ZikO | last post by:
Hi there. I have a problem. I have created nested classes but don't know how to access to inner classes. I know I can create objects: Hen Object; Hen::Nest ObjectNest; Hen::Nest::Egg...
4
by: Boris Yeltsin | last post by:
OK, on my Master Page I have a control: <a id="hypTabAccount" href="#" runat="server">Account</a> Now, in the code-behind (Root.master.vb) I can refer to it simply thus: ...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.