- 1
Friend function
For what is used friend function ? Example in real code pls ...
3 Antworten
+ 2
try this code :-
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() {
regVar = 0;
};
private:
int regVar;
friend void someFunc(MyClass &obj); // Declairing friend function from outside the
// class and pass the class object reference
// as parameter to access its private variables
};
void someFunc(MyClass &obj) { // Now writing the function code (outside the class )
obj.regVar = 42; // wich will take the class object and change its
cout << obj.regVar; // private variable and print it
}
int main() {
MyClass obj;
someFunc(obj);
}
0
it enables you to access The private member data directly ..
class student
{ private:
int x; int y;
};
friend add(x,y)
{
int sum=0;
sum=xx+yy;
}
0
sorry but it is not valid code... :(