I want to overload operator<<
for my class. Should I add this overloaded definition to the std
namespace? (since the ostream operator<<
is part of the std
namespace) Or should I just leave it in the global namespace?
In short:
class MyClass {
};
namespace std {
ostream& operator<< ( ostream& Ostr, const MyClass& MyType ) {}
}
OR
class MyClass {
};
std::ostream& operator<< ( std::ostream& Ostr, const MyClass& MyType ) {}
Which is more appropriate and why? Thanks in advance for your responses.
The rules are applied in order. An overloaded operator will be used if either of the two rules is satisfied. When operator overloading is achieved using non-member function there are two cases to be considered: the overloaded operator uses only the public interface of the class(es) involved in the overloading, or.
Not everything can be overloaded as a member function Because the overloaded operator must be added as a member of the left operand. In this case, the left operand is an object of type std::ostream. std::ostream is fixed as part of the standard library.
The purpose of operator overloading is to provide a special meaning of an operator for a user-defined data type. With the help of operator overloading, you can redefine the majority of the C++ operators. You can also use operator overloading to perform different operations using one operator.
Explaination. Overloaded assignment operator does the job similar to copy constructor and is required to be overloaded as member function of the class.
You should put the operator overload in the same namespace as your class.
This will allow the operator to be found during overload resolution using argument-dependent lookup (well, actually, since ostream
is in namespace std
, the overload overload would also be found if you put it in namespace std
, but there is no reason to do that).
From the point of view of good design practices, the operator overload is more a part of your class's interface than the interface of ostream
, so it belongs in the same namespace as your class (see also Herb Sutter's Namespaces and the Interface Principle).
From the point of view of writing standards-compliant and portable code, you can't put the operator overload into namespace std
. While you can add template specializations for user-defined entities to namespace std
, you can't add additional function overloads.
Don't add it to the std
namespace, place it in the same namespace as your class. The purpose of a namespace is to prevent collisions. The standard says
17.4.3.1 Reserved names
It is undefined for a C++ program to add declarations or definitions to namespace std or namespaces within namespace std unless otherwise specified. A program may add template specializations for any standard library template to namespace std. Such a specialization (complete or partial) of a standard library template results in undefined behavior unless the declaration depends on a user-defined name of external linkage and unless the specialization meets the standard library requirements for the original template.
Don't add to the standard namespace. Reason : If everybody did this, the standard namespace would have heaps of name clashes, which defeats the purpose of a namespace.
Your objective is for your class to be "ostream-able". It does not need to be in the standard namespace to do that. As long as it is in whatever namepsace your class is declared in, you're fine. Putting it in the standard namespace would be bad practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With