You can't access foo and bar without an object. Each Clock has its own foo and bar. Which ones are you trying to access? Which Clock's?
1 2 3 4 5 6 7 8 9 10 11 12 13
class Clock {
public:
friendvoid regulateTime(Clock& obj);
//some member functions...
private:
int foo, bar;
};
void regulateTime(Clock& obj)
{
// now you can access obj.foo and obj.bar just fine
}
ALTHOUGH!!!
Why is this a friend function? Wouldn't it make more sense to make it a member function?
Fairly simple: after our constructors and destructors you have a function that lets you see the time for an object, one that (re)sets time for an object, one that will increase time and the three member variables.
regulateTime() looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
void regulateTime() {
if (mySecond >= 60) {
int s = mySecond % 60;
myMinute += mySecond / 60;
mySecond = s;
if (myMinute >= 60) {
int m = myMinute % 60;
myHour += myMinute / 60;
myMinute = m;
}
}
}
And its purpose is to be used within other member functions. I wanted to merely try to get familiar with friendship in C++ which is why I made it a friend. Initially, it's naturally a private member function.