Operator Overloading

Hey Guys I have a question on operator overloading. I need to write an operator >> which suppoused to work with string. My question is, can I even do it, I mean if the compiler already knows how to work with cin>> (string type), can a write something else?

This is the code i wrote, but i doesnt work..

h file:

#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <string>
#include <climits>
using namespace std;

class Monom{
private:
long m_Nom;
long m_Denom;
long m_Power;

public:

Monom();//Constructor
~Monom();//Destructor
friend istream& operator >> (istream &is,string s);/// Defining >> operator
friend ostream& operator << (ostream &os, const Monom &m);/// Defining << operator

#endif

end of h file
-----------------------------
cpp file:

#include"Monom.h"

Monom::Monom()
{
cout<<"const!\n";
}

istream& operator >> ( istream &is, string s )
{

int startPlace = 0;
int endPlace = 0;
Monom m;

for(int j = 0; j < s.length(); j ++ )///Loop over the whole string, which helps to devide it to monoms
{
startPlace = s.find('/');/// Finds the derative symbol
for(int i = 0; i < startPlace; i ++ )/// Loop which converts the string to nominator
m.m_Nom = m.m_Nom*10 + s[i] - '0';

endPlace = s.find('x');
for(int i = startPlace; i < endPlace; i ++ )/// Loop which converts string to denominator
m.m_Denom = m.m_Denom*10 + s[i] - '0';

s = s.substr(endPlace);/// Cutting the string till 'x'

if(s[endPlace + 1] != '-' )/// Checks if the power isn't negative
{
startPlace = endPlace + 1;
int i = startPlace;
while((s[i] != '+') || (s[i] != '-'))/// Loop which converts the power to long, until it meets + or -
{
m.m_Power = m.m_Power*10 + s[i] - '0';
i++;
}
}
else
{
startPlace = endPlace + 2;
int i = startPlace;
while((s[i] != '+') || (s[i] != '-'))/// Loop which converts the power to long, until it meets + or -
{
m.m_Power = m.m_Power*10 + s[i] - '0';
i++;
}
}

}/// End of major for loop
return is;
}/// End of function

ostream &operator << (ostream& os,const Monom &m)
{
return os<<m.m_Nom<<"/"<<m.m_Denom<<"x"<<m.m_Power;
}

Monom::~Monom()
{
cout<<"dis!\n";
}

void main()
{
string s;
cin>>s;
cout<<s;
}

end of cpp file
It is possible to define it because the existing one is in the std:: namespace, but if you define it you won't be able to use either one because the compiler will be confused as to which one you want to use.

See this example which does not compile, notice that the error occurs in function f():
http://ideone.com/0M5sS4
> I need to write an operator >> which suppoused to work with string.
> My question is, can I even do it, I mean if the compiler already knows how to work with cin>> (string type),
> can a write something else?

Technically, you can. The operators << and >> provided by the library are templates;
a non-template function that you write will trump the template.

The real question is: should you be doing it?
1. It would be a gross violation of the principle of least surprise.
2. Some other completely unrelated part of the code could break.
3. You don't need to write such an overloaded operator; you could very well write:
std::istream& my_fancy_read_string( std::istream& stm, std::string& str ) ;
and use that instead.
1
2
std::string str ;
if( my_fancy_read_string( std::cin, str ) ) { /* ... */ }



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

std::ostream& operator<< ( std::ostream& stm, const std::string& s )
{ return stm << s.c_str() << " [size:" << s.size() << ']' ; }

std::istream& operator>> ( std::istream& stm, std::string& s )
{ return std::getline( stm, s ) ; }

int main()
{
    std::string str ;
    std::cout << "enter a line containing whitespace: " ;
    std::cin >> str ;
    std::cout << "you entered: " << str << '\n' ;
}


http://ideone.com/S1i2nc
Topic archived. No new replies allowed.