Friendship in C++ class not working.....

closed account (z16XoG1T)
I declared a class called FOX and set a friend to it but it won't let me access the private member of the class.

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <conio.h>
using namespace std;
class FOX
{
public: static int x, y;
private: static int p;
friend FOX duplicate (FOX);
      };
int main()
{
if(getch() == 13)cout << FOX::p;
system("PAUSE");
return 0;    
}


To be more precise, the code fails to compile and says "class "FOX" is implicitly friends with itself".

But I read that the only way you can access private and protected members of a class are through the keyword friend.

Some guidance is definitely needed with classes.....
Last edited on
closed account (z16XoG1T)
Also, I got this info from this web site, but it doesn't work.

And the C++ article explicitly claims that the above code will work, but it doesn't allow access nor does it even compile.
Which website??
closed account (z16XoG1T)
The one we're on right now.
When you have a friend class or function, you're accessing the private/protected members of another class. If you want access to the private/protected members of your own class, just make them public or write get/set functions.

Also, I'd suggest that you don't use all-caps names for your classes: all-caps is generally taken to mean a macro.
closed account (z16XoG1T)
But this site says you can access the values of private/protected members in the same class using the friend keyword.

That's what my whole point and question is about.

I DON'T want to access members of another class I want to access members of the same class that are private/protected.
Last edited on
closed account (z16XoG1T)
And does any one know much about use in pointers, templates, polymorphism and encapsulation?
closed account (z05DSL3A)
helpme wrote:
But this site says you can access the values of private/protected members in the same class using the friend keyword.

Does it?

In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends.

Friends are functions or classes declared with the friend keyword.

If we want to declare an external function as friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within the class, and preceding it with the keyword friend:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

class FOX
{
public: 
    FOX(int x =0):p(x) {};
private: 
    int p;

    //allow the non-member get_Fox_P() access to private parts.
friend int get_Fox_P(const FOX &f);
      
};

//This is NOT amember function of FOX
int get_Fox_P(const FOX &f)
{
    return f.p;
}

int main()
{
    FOX foo(10);

    // This line will not compile because FOX::p is private
    std::cout << foo.p << std::endl;

    //as get_Fox_P() is a friend of FOX it has access to p
    std::cout << get_Fox_P(foo) << std::endl;

    return 0;    
}


Topic archived. No new replies allowed.