6

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:

  • innerClass can access all the members of outerClass by default.
  • For outerClass to access private members of innnerClass we need to make outerClass as friend class to innerClass.

Please help in pointing out the mistake in code and also if the points I understood are correct.

sergej
  • 17,147
  • 6
  • 52
  • 89
Mayank Jain
  • 2,504
  • 9
  • 33
  • 52
  • Here's [a hint on definition of inner classes](http://stackoverflow.com/questions/8693590/how-to-make-an-inner-class-without-putting-the-definition-of-inner-class-to-pare) outside the outer class definition – grek40 Oct 12 '16 at 14:33

3 Answers3

5

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Sorry that was a trivial mistake. I correct it but even now I am not able to access private member `b` of innerClass in print function. – Mayank Jain Oct 12 '16 at 14:36
5

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;

sergej
  • 17,147
  • 6
  • 52
  • 89
0

I think you are confusing with nested class and friend class
You can use friend class not using nested class and
you can use nested class not using friend class

Here are some example:

class A {};
class B {};

Here the A class knows the B class, but the B cannot know the A class. So you need to tell the A class that the B class is exist.
This doing thing, is called Forward Declaration
So:

class B;       // forward declaration
class A {};    // A knows the B
class B {};    // B knows the A
Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44