samedi 27 octobre 2018

How should I use a friend declaration in a nested class properly?

For example, suppose I made a code like :

class A
{
private:
    class B
    {
    private:
        int a;
        friend int A::foo(B &b);
    };
    int foo(B &b)
    {
        return b.a;
    }
};

Since a in B is private, to use a in function foo of A, I would use a friend so that foo can actually access a. However this code gives error that it cannot access a. What is the problem of the code, and how should I change the code while keeping a private and A not being friend of B? Or is there a better way?

Aucun commentaire:

Enregistrer un commentaire