-
November 7th, 2010, 06:03 PM
#1
Friend classes
I thought friend classes were able to access the private and protected members. So, in the following test example why am I am not able to write something like:
Code:
A a1;
int p = 15;
cout<<a1.function13(p)
Code:
class A
{
public:
int function1(int x) { return 2*x; }
};
class B
{
public:
double function13(int x) { return x/2; }
friend class A;
};
-
November 7th, 2010, 06:31 PM
#2
Re: Friend classes
The fact that A is a friend of B doesn't change the fact that A has no member function named function13().
You could, however, do this for instance:
Code:
class A
{
public:
A() : secret(666) {}
int function1(int x) { return 2*x; }
friend class B;
private:
int secret;
};
class B
{
public:
double function13(int x) { return x/2; }
void traitor()
{
std::cout << "a_member's secret is " << a_member.secret << std::endl;
}
private:
A a_member;
};
Last edited by Eri523; November 7th, 2010 at 06:47 PM.
Reason: Corrected code
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 7th, 2010, 07:53 PM
#3
Re: Friend classes
I thought friend classes were able to access the private and protected members
The 'friend' keyword should be avoided at all times. As soon as you need it, it means you have a design flaw.
-
November 8th, 2010, 08:34 AM
#4
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|