Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friend function cannot access private function if class is under a namespace

I have a class inside a namespace and that class contains a private function. And there is a global function. I want that global function to be the friend of my class which is inside the namespace. But when I make it as a friend, the compiler thinks that the function is not global and it is inside that namespace itself. So if I try to access the private member function with global function, it doesn't work, whereas if I define a function with the same name in that namespace itself it works. Below is the code you can see.

#include <iostream>
#include <conio.h>

namespace Nayan
{
   class CA
   {
   private:
      static void funCA();
      friend void fun();
   };

   void CA::funCA()
   {
      std::cout<<"CA::funCA"<<std::endl;
   }

   void fun()
   {
      Nayan::CA::funCA();
   }

}

void fun()
{
   //Nayan::CA::funCA(); //Can't access private member
}


int main()
{
   Nayan::fun();
   _getch();
   return 0;
}

I also tried to make friend as friend void ::fun(); And it also doesn't help.

like image 636
user389679 Avatar asked Jul 12 '10 17:07

user389679


People also ask

Can friend function access private data of the class?

A friend function cannot access the private and protected data members of the class directly. It needs to make use of a class object and then access the members using the dot operator. A friend function can be a global function or a member of another class.

Can a private member be accessed by a function which is not a member function?

In C++, a friend function or friend class can also access private data members. So, is it possible to access private members outside a class without friend? Yes, it is possible using pointers.

Can friend function manipulate private members?

Yes, 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.

Can friend function access public members of a class?

Friend function can access all private, protected and public data members of a class. So option D is right. Friend function can access protected and public members of both classes and it can also access private members by using object of that class.


2 Answers

You need to use the global scope operator ::.

void fun();

namespace Nayan
{
    class CA
    {
    private:
        static void funCA();
        friend void fun();
        friend void ::fun();
    };

    void CA::funCA()
    {
        std::cout<<"CA::funCA"<<std::endl;
    }

    void fun()
    {
        Nayan::CA::funCA();
    }

}


void fun()
{
   Nayan::CA::funCA(); //Can access private member
}
like image 162
Brian R. Bondy Avatar answered Oct 22 '22 21:10

Brian R. Bondy


The fun() function is in the global namespace. You need a prototype:

void fun();

namespace Nayan
{
    class CA
    {
    private:
        static void funCA() {}
        friend void ::fun();
    };

}

void fun()
{
    Nayan::CA::funCA();
}
like image 4
Hans Passant Avatar answered Oct 22 '22 22:10

Hans Passant