I'm really thankful to you. You've given me a new concept. I'll clarify everything about this new concept from my teacher.
Actually my concept is:
In order for a function (operator or otherwise) to be a member function, it must have a
first parameter of the class type. The << and >> operators require that the left (first)
parameter be the stream (file) and the right (second) parameter be the class object.
Therefore it is not possible for the operator<< or operator>> functions to have a first
parameter of the class type. A friend function is the only way of implementing an
overloaded operator<< or operator>> function.
You are not wrong on this concept. Vlad is showing you that it is possible to find an alternate solution, however it requires an extra public function. I believe the answer that your instructor is fishing for is this:
If you define an overloaded operator inside of a class, this needs to be on the left. You cannot define an overloaded operator inside the class that puts this on the right. (with the exception of operator++)
The common alternative is to define the overloaded operator outside of the class. This allows us to put the class on the right, however to access private members, the function must be given friend privileges.
Vlad proposed a solution where he provided an accessor function that could only be called with what would normally be the left-hand-side of the operator. It's a brilliant alternative, but I don't think it's easier or clearer than providing friend privileges. Vlad is the kind of guy that takes questions very literally and I'm pretty sure that he's memorized the entire C++ standard.
A friend function is the only way of implementing an overloaded operator<< or operator>> function.
It happens frequently that data that would be printed via operator<< is also available via the public interface of the class. For instance the Int class as defined above is practically useless with no way to access it's value.
In the code below you'll notice that operator<< is neither a friend nor a member of Int, but still manages to work.
So cire, what is happening in your code is that you're tye casting an object directly into an int and that's giving the value of private member through the operator function ()?
You guys have used a brilliant way. I would be very thankful to you guys if you can recommend me a book for learning and mastering C++. Please...
Request to cire, vlad and Stewbond.
#include <iostream>
class Int
{
public:
Int( int x = 0 ) : i( x ) {}
operatorint() const { return i ; }
int get() const { return i ; }
private:
int i;
} ;
std::ostream& operator<<(std::ostream& os, const Int & theInt)
{
return os << theInt.operatorint() ;
// or, same principle:
// return os << theInt.get() ;
}
int main()
{
Int i = 9 ;
std::cout << i << '\n' ;
}
Coincidentally, the following would also work as providing operator int for an Int means it will match an existing overload of operator<<.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
class Int
{
public:
Int( int x = 0 ) : i( x ) {}
operatorint() const { return i ; }
private:
int i;
} ;
int main()
{
Int i = 9 ;
std::cout << i << '\n' ;
}