Topic: Overload resolution when deriving from string class
Author: "P.Wolff" <wolff@corporateu.com>
Date: 1999/06/07 Raw View
The code below compiles in both Visual C++ 5.0 and 6.0. but the code
invokes the STL standard operator:
istream & operator >> (istream &, string &)
Wouldn't the overload resolution rules indicate that the user defined
operator should be chosen, since it is a better match on the second
argument?
Is this a Microsoft bug ?
I know Stroustrup recommends that the string class not be used as a base
class (Stroustrup, C++ Programming Language, 3rd edition, Section 20.3),
but the question is, what should a conforming compilere do ?
Thanks.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class mystring : public string
{ public:
mystring(char *p="") : string(p)
{}
};
istream & operator >> (istream & i, mystring & s) // use defined
function
{ cout << "in the operator" << endl;
return i;
}
void main()
{
ifstream in("mary.txt");
mystring s;
in >> s; // This line compiles in VC++ 5.0 and 6.0
// but calls the STL standard operator
// operator >> (istream &i, string &s)
// instead of the user defined operator
// istream & operator >> (istream &, mystring &)
cout << s;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]