Operator Overloading !!

I need to know can i change cout operator ""<<"" with "<"(single one) with operator overloading or anyting else ?
Yes you can.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>     

// Use templates to redirect < to << for all types.
template <typename T>
std::ostream& operator<(std::ostream &os, const T& value)
{
	return os << value;
}

// This is for functions such as std::endl.
std::ostream& operator<(std::ostream &os, std::ostream& (*f)(std::ostream&))
{
	return f(os);
}

int main() 
{
	std::cout < "abc" < 123 < std::endl;
}
Last edited on
Topic archived. No new replies allowed.