hi guys,
I just finished reading the tutorial and im trying to understand some object programming concepts so be slow with me :D
I have been trying to imitate some things i already see on the language.
In this case I wanted to imitate the following:
|
cout << "some text" << "more text";
|
so, reading around in the references i saw that
1) cout is an object
2) << is an operator function
3) that operator is overloaded
so consider this little code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include <sstream>
using namespace std;
class base {
public:
void operator < (const string text){
cout << text << endl;
}
}print;
int main(){
print < "this is a test";
cin.get();
return 0;
}
|
all is nice up to now.
The problem is cant imitate the following:
|
print < "this is a test\n" < "and this is why it fails";
|
since i have to create an overloaded operator that handles this case.
But how can i do that?
I have been reading this:
http://cplusplus.com/reference/iostream/ostream/operator%3C%3C/
and the closest that i can think that does that is:
ostream& operator<< (ostream& ( *pf )(ostream&));
ostream& operator<< (ios& ( *pf )(ios&));
ostream& operator<< (ios_base& ( *pf )(ios_base&));
|
But i dont quite get it, and havent been able to make it work.
Any help would be appreciated.