-
Friend Class
Hello everyone. I've never used friend classes before, but just today I found (a very large) use for them in a project of mine. Anyway, what im doing is this:
Code:
class test
{
public:
int tester;
};
class Cblah
{
public:
int type;
friend test;
};
...(later on)
Cblah bob;
...(later)
if(bob.tester==1)
{
...
}
But when I try to compile it, it tells me that 'tester' is not a member or Cblah. Now, it isn't, but shouldn't the friend to test allow it to access 'tester'? I may be totally wrong about how friends work, so could someone tell me either what im doing wrong or set friends straight? Thanks.
-
Yup, I think your idea of friend classes is totally wrong.
Friend classes/functions can access the private members of a class. That's basically it. If you specify "friend class Test" in class Blah, then it means that Test can access and manipulate private, protected and public members/functions of class Blah.
Code:
class Test
{
public:
void CallBlahFunc(Blah& b)
{
b.BlahFunc(); // valid, coz it's a friend of blah
{
}
class Blah
{
friend class Test;
private:
void BlahFunc(void);
}
Test t;
Blah b;
b.BlahFunc(); // wont work, private function
t.CallBlahFunc(); // works no problem
I hope this clears it up a bit.
cheers
U.
-
Thanks, understand them now!
-
I think what you were thinking is inheritance