Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - why difference between temp and named variable?
This program fails as written, but a fix is shown in comments: #include <iostream.h> #include <strstream.h> struct mine { int a; }; ostream& operator << (ostream& out, mine& m) { return out << m.a; } istream& operator >> (istream& in, mine& m) { return in >> m.a; } int main() { mine mm; //these two lines work // istrstream in("106"); // if (in >> mm) //but this one line alternative does not work if (istrstream("106") >> mm) cout << "mm= " << mm << endl; else cout << "extract fails" << endl; } The bad version gets this error message: cannot generate copy constructor for class 'istream' because of private 'ios' Why does the compiler need to generate a copy constructor? The code creates a temporary istrstream, passes it by reference to operator>>(), then uses the returned reference to call member function ios::operator void*(). Neither passing by reference nor calling a member function involves copy constructing. Why does the commented-out version work? Giving the istrstream a name instead of using a temp should not influence the need for a copy constructor. Apr 22 2004
Hello Steve, Steve Strand wrote...#include <iostream.h> #include <strstream.h> struct mine { int a; }; ostream& operator << (ostream& out, mine& m) { return out << m.a; } istream& operator >> (istream& in, mine& m) { return in >> m.a; } int main() { mine mm; //these two lines work // istrstream in("106"); // if (in >> mm) //but this one line alternative does not work if (istrstream("106") >> mm) Apr 23 2004
Thank you Heinz! Your answer is helpful and insightful. Steve Apr 23 2004
|