Can somebody explain why this prints 10 both times?
Code:
#include <iostream>
using namespace std;
class fred {
int _i;
public:
fred (int v) { _i = v; }
friend ostream &operator<< (ostream &o, const fred *f);
friend ostream &operator<< (ostream &o, const fred &f);
};
ostream &operator<< (ostream &o, const fred *f)
{
return o << f->_i;
}
ostream &operator<< (ostream &o, const fred &f)
{
return o << f._i;
}
int main (int argc, char *argv[])
{
fred *f = new fred (10);
fred f2 (20);
cout << "f: " << f << endl;
cout << "f2: " << f << endl;
delete f;
return 0;
}