473,725 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Oute r *o) { return o->outer_data; };
};

Outer(int data) { outer_data = data; };
int outer_func(Inne r *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(Inne r *) can't access
inner_data. Why can inner_func(Oute r *) 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 2428
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(Oute r *o) { return o->outer_data; };
};

Outer(int data) { outer_data = data; };
int outer_func(Inne r *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(Inne r *) can't access
inner_data. Why can inner_func(Oute r *) 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(Inne r *) can't access
inner_data. Why can inner_func(Oute r *) 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(Inne r *) can't access
inner_data . Why can inner_func(Oute r *) 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
9760
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 reduced code example: class List { private: void *rootNode; class Iterator {
8
3944
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 private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
9
1538
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
2080
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 overflow. All I know is that the program is either stopped due to an exception at the end of Main or has exited after the Stack Overflow exception. (Both cases are Debug builds running in the debugger.) After a lot of trial and error (mostly...
6
2079
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
16913
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. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
6
1809
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 = new Test1();
5
2192
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 ObjectEgg; and have access to particular elements
4
3992
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: hypTabAccount.InnerText = "blah" Now, what I want is the same in a content page that uses the Master Page. I have a Master Type in my Content page:
0
8874
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9393
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6695
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6000
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4506
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4775
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.