an****@gmail.com wrote:
ok its this part which is taxing me
friend class alarm_clock_maintainer;
};
class alarm_clock_maintainer;
why do you make the same name of class as you do with the friend inside
a class..
wouldent this mean a class inside a class :o
Firstly, read up about Usenet. This is not a forum.
Secondly, classes within classes are perfectly legal, but that's *not*
what's happening here.
From the original example:
This is called a forward declaration. It tells the compiler that a class
named "alarm_clock_maintainer" exists, but doesn't tell it anything
about the details of the class. Before you can use this type the proper
class definition must be visible, but for our "friend" declaration we're
not using any information other than the class name, so this is good enough.
class alarm_clock_maintainer;
This is the definition of a class that happens to have private stuff.
All the type names listed in "friend" declarations are allowed to access
our private stuff directly without using the public class interface we
provide.
The friend declaration can be in any part of the class definition, it
doesn't need to be in the public section. It always means the same
thing: the type named in the declaration is allowed to access our
private stuff.
class alarm_clock
{
private:
spring source;
gearbox gbox1;
gearbox gbox2;
// ... other internal stuff
public:
// ... other public stuff
friend class alarm_clock_maintainer;
};
Here's the actual definition of alarm_clock's friend. I think the rule
is that friends have to be defined in the same translation unit, but I
could be wrong about that.
Now, when you call "my_alarm_clock_maintainer.inspect(my_alarm_clock) "
the inspect() function is allowed to examine all of the private members
of alarm_clock, e.g. alarm_clock::gbox1. If alarm_clock didn't have the
friend declaration then this class wouldn't be allowed to do that.
class alarm_clock_maintainer;
{
private:
// ... internal stuff
public:
alarm_clock& inspect(alarm_clock& clock);
// ... other public stuff
};
Jacques.