问题
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?
回答1:
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.
回答2:
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.
回答3:
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?
来源:https://stackoverflow.com/questions/7824396/overloading-operator-for-a-private-enum