1

My code:-

#include<iostream>
using namespace std;

class a{
   private:
      int x;
   public:
      a(int data)
      {
         x=data;
      }
      friend void printPrivateMember(a);
};

void printPrivateMember(a obj)
{
   cout<<obj.x; //I can access private data member by an object inside this function.
}

int main()
{
   a obj1(5);
   printPrivateMember(obj1);
   cout<<obj1.x; //this gives error
   return 0;
}

I wanted to know as to how can I access a PRIVATE data type by an object in the friend function but cannot do so in main.

When I read about access specifier . It was specified that private can be accessed by only member functions (I don't have a problem with the friend function) and not by the object of that class. I wanted to know as to what difference is there because of which I can access private member by an object in one case and cannot do so in another. The same is applicable for copy constructor.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Echo
  • 570
  • 1
  • 7
  • 21

6 Answers6

1

That's exactly what friend functions do: any friend function of a class can access it's private members. Since your printPrivateMember is declared as a friend of a, it can access it's private x member. Since main is not a friend function, it can't.

Forestalling a question about declaring main as friend, this question covers it.

Community
  • 1
  • 1
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • I wanted to know how does we access private member by an object of the class. Don't we need a member function to access them? – Echo Oct 21 '15 at 07:27
  • @SudeshnaBora, no, you can access them directly from anywhere if they are public, and from friend functions or friend classes even if those members are not public. – SingerOfTheFall Oct 21 '15 at 07:28
  • @SudeshnaBora Yes, you need a member function **or** a `friend`. But that's just the behavior you observed in your example program. – cadaniluk Oct 21 '15 at 07:29
1

Because friends could do that.

$11/1 Member access control [class.access]

(emphasis mine)

1 A member of a class can be

(1.1) — private; that is, its name can be used only by members and friends of the class in which it is declared.
(1.2) — protected; that is, its name can be used only by members and friends of the class in which it is declared, by classes derived from that class, and by their friends (see 11.4).
(1.3) — public; that is, its name can be used anywhere without access restriction.

Community
  • 1
  • 1
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0

As you correctly observed, only member functions (including constructors and destructors) and friend functions and classes may access you're privates. That's the purpose of friends: they provide an exception (not std::exception) to the encapsulation mechanism.

Now you may think about whether this breaks encapsulation or actually stabilizes it.

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
0

if you want to access private member, you'd better use a public function like:

class a {
 private:
   int m;
 public:
   int getM() {
     return m;
   }
};
YC Yu
  • 1
  • 1
0

Your use of the phrase not by the object of that class makes me think that you are unclear on the access rules. The access rules don't apply to the objects but who can access member variables and member functions of the objects.

A member variable of a class can be accessed in a function -- which can be a member function of the class, a member function of another class, or a global function.

It can also be accessed in the global space, e.g. to initialize a global variable.

A friend declaration in a class changes the default access rules that are in place by use of private, protected, and public access specifiers.

A function declared a friend of a class can access all the members of all instances of the class.

The answer by songyuanyao cites the section of the standard that provides more details on the subject.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I wanted to know as to how can I access x (data member) by the object of the class (obj) in the friend function . But could not do so in main. I was of the assumption that objects cannot access private member directly by only via member function. – Echo Oct 21 '15 at 07:38
  • That's simple. That's what you get when you declare a function a `friend` of a class. Since `main` is not declared a `friend` of the class, you can only access the `public` member variables and member functions of the class in `main`. – R Sahu Oct 21 '15 at 07:40
  • So member access has got nothing to do with object of the class. I have no idea how o get this weird notions. Thank you so much. – Echo Oct 21 '15 at 07:43
0

This function should be public, so that you can access it through main().

void print(){
    /**print or return your private variable here**/
}
Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64