yes you can. There may be many reasons for that such as access to private static members or there might be a global instance of sample
. It is also possible that fun
creates an instance of sample
and grab its privates.
working example for function creating instance and doing stuff with it :
#include <iostream>
class sample {
private:
int x;
public:
friend void fun();
};
void fun()
{
sample s;
s.x = 555555555;
std::cout << s.x;
}
int main(){
fun();
return 0;
}
example with global instance:
#include <iostream>
#include <string>
class sample {
private:
int x;
public:
friend void fun();
};
sample s;
void fun()
{
s.x = 555555555;
std::cout << s.x;
}
int main(){
fun();
return 0;
}
example with private static member :
#include <iostream>
#include <string>
class sample {
private:
int x;
static const int w = 55;
public:
friend void fun();
};
void fun()
{
std::cout << sample::w;
}
int main(){
fun();
return 0;
}