I am trying to understand concept of friendship in nested classes but I am not getting the concept properly. I have written a sample program to understand it but the program is not working
#include<iostream>
using namespace std;
class outerClass
{
private:
int a;
public:
class innerClass;
bool print(innerClass);
};
class innerClass
{
friend class outerClass;
private:
int b;
public:
innerClass() =default;
};
bool outerClass::print(outerClass::innerClass obj)
{
cout<<"Value of b in inner class is:"<<obj.b;
}
int main()
{
outerClass in;
outerClass::innerClass obj;
obj.b=5;
in.print(obj);
}
I am getting below errors:
try.cpp: In member function ‘bool outerClass::print(outerClass::innerClass)’:
try.cpp:26:6: error: ‘obj’ has incomplete type
try.cpp:11:15: error: forward declaration of ‘class outerClass::innerClass’
try.cpp: In function ‘int main()’:
try.cpp:34:28: error: aggregate ‘outerClass::innerClass obj’ has incomplete type and cannot be defined
As I read through articles on internet I learnt following points please comment on them if they are correct or not:
Please help in pointing out the mistake in code and also if the points I understood are correct.
If the declaration of a nested class is followed by the declaration of a friend class with the same name, the nested class is a friend of the enclosing class.
A friend class in C++ can access the private and protected members of the class in which it is declared as a friend. A significant use of a friend class is for a part of a data structure, represented by a class, to provide access to the main class representing that data structure.
A friend function can be friendly to 2 or more classes. The friend function does not belong to any class, so it can be used to access private data of two or more classes as in the following example. The friend functions can serve, for example, to conduct operations between two different classes.
The major disadvantage of friend functions is that they require an extra line of code when you want dynamic binding. To get the effect of a virtual friend , the friend function should call a hidden (usually protected ) virtual member function. This is called the Virtual Friend Function Idiom.
The line class innerClass;
within outerClass
is a forward declaration to a class
that you never define.
Hence outerClass::innerClass
is an incomplete type.
The separate innerClass
definition that starts with
class innerClass
{
is a completely different class
to the forward declared class.
There's nothing wrong with your friend class outerClass;
statement within the defined innerClass
.
If you want to define innerClass
outside of outerClass
, here is how to do it:
class outerClass
{
class innerClass; // forward declaration
};
class outerClass::innerClass // definition
{
};
The rest is OK, except of obj.b=5
. The class outerClass
is allowed to access innerClass::b
, the function main()
is not.
innerClass can access all the members of outerClass by default.
Right. From the standard [class.access.nest]:
A nested class is a member and as such has the same access rights as any other member.
For outerClass to access private members of innnerClass we need to make outerClass as friend class to innerClass.
Right. From the standard [class.access.nest]:
The members of an enclosing class have no special access to members of a nested class;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With