Если вы хотите получить только класс a
из B
, вам нужна функция получения.Это должен быть самый простой способ.
class B
{
private:
int a;
public:
// provide getter function
const int& getMember_a()const { return a; }
};
и в функции foo
const int& foo(const B &b)const
{
return b.getMember_a(); // call the getter to get the a
}
Относительно вопроса вашего кода;в строке friend int A::foo(B &b);
в B
классе, он не знает, что функция A::foo
.Поэтому нам нужно переслать объявление int foo(B &);
перед классом B
.Тогда вопрос;знает ли A::foo(B &)
о B
.Тоже нет.Но, к счастью, C ++ позволяет иметь неполный тип, также объявляя классы вперед.Это означает, что, следуя по пути, вы можете достичь желаемой цели.
class A
{
private:
class B; // forward declare class B for A::foo(B &)
int foo(B &); // forward declare the member function of A
class B
{
private:
int a;
public:
friend int A::foo(B &b);
};
};
// define, as a non-member friend function
int A::foo(B &b)
{
return b.a;
}