c++ - Method Overloading Of >> Binary Operator To Print A String -
i want stringclass class overloading >> binary operator follows:
stringclass teststring; teststring = "test"; printf("%s", teststring >> 5);
and there testtesttesttesttest printed (five times.)
for needs. discourage things, here is.
and yeah, bad static variable. bad practices. cares.
#include <string> #include <cstdio> using namespace std ; const char* operator >> (const string &str, const int n) { static string ret_string ; ret_string.clear() ; (int i=0 ; i<n ; i++) { ret_string += str ; } return ret_string.c_str() ; } int main() { string str="test" ; printf("%s", str>>5) ; return 0 ; }
as can see, define >>
operator return whatever want. warning static variable, no reentrant, blah,blah,blah :) hope helps.
if wish, can enclose operator definition inside class (if not string
class).
** p0w initial code structure.
Comments
Post a Comment