Suppose that you have the following code:
#include <iostream>
template <typename T>
class Example
{
public:
Example() { };
Example(const T &_first_ele, const T &_second_ele) : first_(_first_ele),
second_(_second_ele) { }
friend std::ostream &operator<<(std::ostream &os, const Example &a)
{
return (os << a.first_ << " " << a.second_);
}
private:
T first_;
T second_;
};
int main()
{
Example example_(3.45, 24.6);
std::cout << example_ << "\n";
return 0;
}
Is this the only way to overload the << operator?
friend std::ostream &operator<<(std::ostream &os, const Example &a)
{
return (os << a.first_ << " " << a.second_);
}
In terms of performance, is it the best way to overload it or are there better options to do this implementation?
Aucun commentaire:
Enregistrer un commentaire