473,289 Members | 1,810 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,289 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 2406
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.