Whenever I use cout and cin, I have to use 3 key (shift,2 press < <) .
I tryed to overloading ostream and istream with ,
(comma operator) .
And now everything works well, except cin on int
,float
,double
,char
but it works with char[]
. also I tested tie()
method to tie ostream to istream but stream of cin does not tie to stream of cout.
In fact cin get value but the value does not tie to cout.
thanks so much if you have an idea.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
template < class AT> // AT : All Type
std::ostream& operator,(std::ostream& out,AT t)
{
out<<t;
return out;
}
template < class AT> // AT : All Type
std::istream& operator,(std::istream& in,AT t)
{
in>>t;
return in;
}
int main(){
cout,"stack over flow\n";
const char* sof ( "stack over flow\n" );
cout,sof;
char sof2[20] ("stack over flow\n");
cout,sof2;
int i (100);
float f (1.23);
char ch ('A');
cout,"int i = ",i,'\t',",float f = ",f,'\t',",char ch = ",ch,'\n';
cout,"\n_____________________\n";
cin,sof2; /// okay, it works
cout,sof2; /// okay, it works
cin,i; /// okay it works
cout,i; /// does not work. does not tie to cin
}
stack over flow stack over flow stack over flow int i = 100 ,float f = 1.23 ,char ch = A _____________________ hello // cin,sof2; /// okay, it works hello 50 // cin,i; /// okay it works 100 // does not work and return the first value the smae is 100 Process returned 0 (0x0) execution time : 15.586 s Press ENTER to continue.
by : g++ 5.2.1.
if you wnat to test this code, your gun c++ must be 5.2 or upper; or change ()
initialize to =
for compile on command-line
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp
You code doesn't work for int, float, double, char because in your >> operator you pass argument by value, not by reference. Change it in this way:
template < class AT> // AT : All Type
std::istream& operator,(std::istream& in, AT& t)
{
in>>t;
return in;
}
But as graham.reeds already stated, it's a bad idea to replace << and >> operators with comma in this way, it will mess your code.