Wrox Press C++ Tutorial


Class Members as friends

We have already seen how a function could be declared as a friend of a class. This gave the friend function the privilege of free access to any of the class members. Of course, there is no reason why a friend function cannot be a member of another class.

Suppose we define a class Bottle to represent a bottle:

class Bottle
{
   private:
      double height;       // Bottle height
      double diameter;     // Bottle diameter

   public:
      Bottle(double height, double diameter)
      {
         height = height;
         diameter = diameter;
      }
};

We now need a class to represent the packaging for a dozen bottles, that will automatically have custom dimensions to accommodate a particular kind of bottle. We could define this as:

class Carton
{
   private:
      double length;        // Carton length
      double breadth;       // Carton breadth
      double height;        // Carton height

   public:
      Carton(Bottle aBottle)
      {
         height = aBottle.height;           // Bottle height
         length = 4.0*aBottle.diameter;     // Four rows of ...
         breadth = 3.0*aBottle.diameter;    // ...three bottles
      }
};

The constructor here sets the height to be the same as that of the bottle it is to accommodate, and the length and breadth are set based on the diameter of the bottle so that twelve will fit in the box.

As you know by now, this won't work. The data members of the Bottle class are private, so the Carton constructor cannot access them. As you also know, a friend declaration in the Bottle class should fix this:

class Bottle
{
   private:
      double height;       // Bottle height
      double diameter;   // Bottle diameter

   public:
      Bottle(double height, double diameter)
      {
         height = height;
         diameter = diameter;
      }

      // Let the carton constructor in
      friend Carton::Carton(Bottle aBottle);
};

When we declare a member function as a friend of another class, we need to put the class name and the scope resolution operator with the friend function name to identify it. For this to compile correctly, the compiler needs to have information about the Carton class constructor, so you would need to put an #include statement for the file containing the Carton class definition before the definition of the Bottle class.

Friend Classes

You can also allow all the function members of one class to have access to all the data members of another by declaring it as a friend class. We could define the Carton class as a friend of the Bottle class by writing a friend declaration in the Bottle class definition:

friend Carton;

All function members of the Carton class will now have free access to all the data members of the Bottle class.

Limitations on Class Friendship

Class friendship is not reciprocated. Making the Carton class a friend of the Bottle class does not mean that the Bottle class is a friend of the Carton class. If you want this to be so you must add a friend declaration for the Bottle class to the Carton class.

Class friendship is also not inherited. If you define another class with Bottle as a base, members of the Carton class will not have access to its data members, not even those that are inherited from Bottle.


© 1998 Wrox Press