-1

**If i have friend function in class A and there is no member function in Class A.Then can i access class A useing that Friend function **

if not than how can access that class?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Can you give an example of what you mean here? I don't quite understand what you mean by accessing a class. – Guillaume Racicot Nov 14 '19 at 15:15
  • Can you explain what you mean? A function that is declared `friend` to a class will have access to all the internals of that class, including it's `private` and `protected` member, if that's what you are asking about. If you don't add any `public` member, then only `friend` will be able to interact with this class. – Yksisarvinen Nov 14 '19 at 15:16

1 Answers1

1

Of course you can. For example

#include <iostream>

class A
{
private:
    friend void f( A &a, int x )
    {
        a.x = x;
    }

    friend std::ostream & operator <<( std::ostream &os, const A &a );

    int x = 10;
};

std::ostream & operator <<( std::ostream &os, const A &a )
{
    return os << a.x;
}

int main() 
{
    A a;

    std::cout << "a.x = " << a << '\n';

    f( a, 20 );

    std::cout << "a.x = " << a << '\n';

    return 0;
}

The program output is

a.x = 10
a.x = 20
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335