1

My class has a private enum whose members are being used to index an array of strings, the output of which is written to an output stream.

private:
    enum supportedMessageTypes(CRITICAL = 0, WARNING, INFORMATION);
    string messages[3];

//meanwhile, inside the constructor,
messages[3] = {"Critical error message", 
               "Warning message", 
               "Information message"};

Since I'm going to be using the enum values around my code a lot, I'd like to be able to overload operator<< to perform a lookup of the enum value, match it to the corresponding string in the array, and return that in the following manner:

cout << CRITICAL << ": " << messageText << std::endl;

The problem I have is that supportedMessageTypes is private while overloading of operator<< should be done as a non-member function. I know I can define a friend function that overloads operator<< for my class, but I'm simply not comfortable with breaking encapsulation in that way, and was wondering if anyone knew of a way to overload operator<< without using friend functions or making supportedMessageTypes public?

  • 5
    In this scenario it is perfectly fine to befriend the operator. This is one of the intended use-cases. See [here](http://www.parashift.com/c++-faq-lite/friends.html#faq-14.2) for a detailed explanation. – Björn Pollex Oct 19 '11 at 16:09
  • 1
    If used correctly, friend enhances encapsulation. Here, friend is appropriate. See, http://www.parashift.com/c++-faq-lite/friends.html#faq-14.2 – Paul Manta Oct 19 '11 at 16:22

3 Answers3

3

This isn't possible. You want to accept an argument of type supportedMessageTypes, so it must be visible. There's no way around it.

Also, a friend function wouldn't be bad here; this is one of the intented scenarios for friendship.

Jon
  • 428,835
  • 81
  • 738
  • 806
1

I cannot agree with some of the premises in your question. In particular, when you say that declaring the operator<< as a friend function breaks encapsulation. It breaks encapsulation in exactly the same way as any member function of your type breaks encapsulation. The operator<< is part of your type and has access to the private parts, but that is not a breakage of encapsulation. Note that in C++ not only member functions belong to the interface of the type. Google for interface principle.

class test {
   enum E { ok, nok };
   friend std::ostream& operator<<( std::ostream&, E );
};
std::ostream& operator<<( std::ostream& out, E e ) {
   if ( e == ok ) out << "ok";
   else out << "nok";
   return out;
}

Note that you are not opening your class to anyone, only to one of your functions. If you feel at unease, think of this almost equivalent way of generating the same code:

class test {
    enum E { ok, nok }
    friend std::ostream& operator<<( std::ostream& out, E e ) {
       if ( e == ok ) out << "ok";
       else out << "nok";
       return out;
    }
};

How is that code less encapsulated than your original code?

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

http://ideone.com/gMTZ6

Dispite my efforts, I couldn't find a way to do this without the friend keyword. On the other hand, this does not break encapsulation, as nobody outside of your class can use the function, since they cannot access the type being used in the type lookup. Ergo, you're still fully encapsulated.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158