T
Thomas
hi all,
I have 2 classes, A and B. Each class has a member function that needs to
access an element of the other class. Here's the code:
#include <iostream>
using namespace std;
class B;
class A
{ public:
void AFunction(B& arg_b);
friend void B::BFunction(B&); //allow BFunction to access avar
private:
int avar;
};
class B
{ public:
void BFunction(A& arg_a);
friend void A::AFunction(B&); //allow AFunction to access bvar
private:
int bvar;
};
void A::AFunction(B& arg_b) { cout << arg_b.bvar << endl; }
void B::BFunction(A& arg_a) { cout << arg_a.avar << endl; }
int main()
{ A MyAObject;
B MyBObject;
}
This doesn't compile because B::BFunction(B&) is declared a friend before B
is defined. But swapping classes A and B just makes A::AFunction the
problem...
Is there a way to do this without making the whole classes friends of each
other?
Thanks in advance,
Thomas
I have 2 classes, A and B. Each class has a member function that needs to
access an element of the other class. Here's the code:
#include <iostream>
using namespace std;
class B;
class A
{ public:
void AFunction(B& arg_b);
friend void B::BFunction(B&); //allow BFunction to access avar
private:
int avar;
};
class B
{ public:
void BFunction(A& arg_a);
friend void A::AFunction(B&); //allow AFunction to access bvar
private:
int bvar;
};
void A::AFunction(B& arg_b) { cout << arg_b.bvar << endl; }
void B::BFunction(A& arg_a) { cout << arg_a.avar << endl; }
int main()
{ A MyAObject;
B MyBObject;
}
This doesn't compile because B::BFunction(B&) is declared a friend before B
is defined. But swapping classes A and B just makes A::AFunction the
problem...
Is there a way to do this without making the whole classes friends of each
other?
Thanks in advance,
Thomas