0
how to get Return from a Function created as Private?
1 Answer
+ 1
If you have a private function, then by default there is no way you can access it from outside the class functions. It becomes user's responsibility to provide a work around. There are several ways you can accomplish this:
1) You can create a public function in the same class and execute the private function there and hence return the value.
Example:
class A
{
public:
int b()
{
return c();
}
private:
int c()
{
return 22;
}
};
Now, since b() is a public function, it can be called from any other function. And it will return you the value from c(), which is private.
2) You can create a friend function in 'public' of the class. This function will have access to all the data of the class.
class A
{
int c()
{
return 22;
}
public:
friend void b();
};
void b()
{
int value = c();
}
Here you can see that, b() can call c(), which is a private function from anywhere in the code.