0
What is friend function and how we use it in classes?
too much confused about it
4 odpowiedzi
+ 2
friend is a keyword in c++. If you use on any function or class in other class(let say s class).
the function or class with friend keyword can access all member function including private member of class s1.
+ 2
#include <iostream>
using namespace std;
class MyClass {
public:
int hr;
private:
int regVar;
friend void someFunc(MyClass &obj);
};
void someFunc(MyClass &obj) {
obj.regVar = 42;
cout << obj.regVar;
}
//above you can see someFunc use private member //of class Myclass that is regVar
int main() {
MyClass obj;
someFunc(obj);
}
I have use tutorial example.
0
any example?
0
thanku very much