Question on iostream : c++
This is a discussion on Question on iostream within the c++ forums in Programming Languages category; I don't know why following code is working. #include <iostream> #include <fstream> using namespace std; istream& operator<<(ostream& os, CPerson &str){ return os << CPerson.getName(); /* getName returns const char* */ } int main(void) { CPerson person; //Person is User-Definition Class // not strange cout << person; ofstream ofs("test.txt"); // strange. why do this code work? ofs << person; return 0; } I read the book "Programming C++ third edition" & "The C++ Standard Library - A Tutorial and Reference" again. And I consult the source code which is g++ 4.0 on Mac OSX. ofstream is inherited ostream. but "operator<<" is ...
| c++ comp.lang.c++ usenet archive |
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| #include <iostream> #include <fstream> using namespace std; istream& operator<<(ostream& os, CPerson &str){ return os << CPerson.getName(); /* getName returns const char* */ } int main(void) { CPerson person; //Person is User-Definition Class // not strange cout << person; ofstream ofs("test.txt"); // strange. why do this code work? ofs << person; return 0; } I read the book "Programming C++ third edition" & "The C++ Standard Library - A Tutorial and Reference" again. And I consult the source code which is g++ 4.0 on Mac OSX. ofstream is inherited ostream. but "operator<<" is not member operator, no virtual directive, and ofstream object "ofs" isn't pointer. I can't understand this "polymorphic" action. Maybe type conversion happend? Can anyone please tell me why this is happening? I am using g++4.0 on Mac OSX. I'm Japanese, So please forgive me about my poor English. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
#2
| |||
| |||
| shnya <testmhigashi> wrote: > I don't know why following code is working. The code you posted shouldn't even compile. I've corrected the obvious errors below: ostream& operator<<(ostream& os, CPerson &str){ return os << str.getName(); /* getName returns const char* */ } int main() { CPerson person; //Person is User-Definition Class // not strange cout << person; ofstream ofs("test.txt"); // strange. why do this code work? ofs << person; } > I read the book "Programming C++ third edition" & "The C++ Standard > Library - A Tutorial and Reference" again. > And I consult the source code which is g++ 4.0 on Mac OSX. > > ofstream is inherited ostream. > but "operator<<" is not member operator, no virtual directive, and > ofstream object "ofs" isn't pointer. > I can't understand this "polymorphic" action. > Maybe type conversion happend? An ofstream reference is freely convertible to an ostream reference. Since there is no operator<< that accepts an ofstream (as value or reference and a CPerson (as value or reference,) the compiler uses the operator<< that you provided. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
#3
| |||
| |||
| shnya <testmhigashi> wrote: > I don't know why following code is working. > > #include <iostream> > #include <fstream> > > using namespace std; > > istream& > operator<<(ostream& os, CPerson &str){ > return os << CPerson.getName(); > /* getName returns const char* */ > } This should probably return ostream& instead of istream&, in order to allow chaining of output commands like: cout << person1 << '\n' << person2 << '\n'; > CPerson person; //Person is User-Definition Class > > // not strange > cout << person; > > ofstream ofs("test.txt"); > > // strange. why do this code work? > ofs << person; > [snip] > > ofstream is inherited ostream. Yes, ofstream is a derived class of ostream. > but "operator<<" is not member operator, no virtual directive, and > ofstream object "ofs" isn't pointer. > I can't understand this "polymorphic" action. > Maybe type conversion happend? In C++, the line: ofs << person; is shorthand for: operator<<(ofs, person); Since you are passing the ostream& to operator<< by reference (that is what the '&' means in the function declaration), then that means that *any* class derived from ostream is allowed to be passed to operator<<. Passing by reference is almost like passing by pointer, except that the pointer is always dereferenced, and you cannot change the object that the reference refers to. -- Marcus Kwok Replace 'invalid' with 'net' to reply [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
#4
| |||
| |||
| In article <1181250320.122666.49440@g37g2000prf.googlegroups.com>, shnya <testmhigashi> wrote: > > using namespace std; > > istream& > operator<<(ostream& os, CPerson &str){ > return os << CPerson.getName(); > /* getName returns const char* */ > } assuming this operator << is really ostream & operator << (ostream &os,const CPerson &str) { return os << str,getName(); } all should work -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
#5
| |||
| |||
| On Jun 8, 12:58 pm, Carl Barron <cbarron...@adelphia.net> wrote: > In article <1181250320.122666.49...@g37g2000prf.googlegroups.com>, > > shnya <testmhiga...> wrote: > > > using namespace std; > > > istream& > > operator<<(ostream& os, CPerson &str){ > > return os << CPerson.getName(); > > /* getName returns const char* */ > > } > > assuming this operator << is really > ostream & operator << (ostream &os,const CPerson &str) > { > return os << str,getName(); > > } > > all should work > Dear Daniel T. and Dear Carl Barron. I'm sorry. This is my mistake. Yes, your code is what I want to write. I will take care when I post next. Thank you. Dear Marcus Kwok. Thank you. I want to hear this reason. I thought that I should need following code. ofstream & operator<<(ofstream& os, CPerson &str){ return os << CPerson.getName(); } Because I thought that reference was similar to the value type, but reference wasn't copied and point same object. So I check following code. ---- #include <iostream> using namespace std; class Base { string name_a; public: explicit Base(string i) :name_a(i) {} virtual string getName(){ return name_a;} }; class Derived : public Base { string name_b; public: explicit Derived(string i, string j) :name_b(i), Base(j) {} virtual string getName(){ return name_b;} }; void printValue(Base a){ cout << a.getName() << endl; } void printRef(Base &a){ cout << a.getName() << endl; } void printPtr(Base *a){ cout << a->getName() << endl; } int main(void){ Base a("class a"); Derived b("class b", "class a"); printValue(a); printValue(b); printRef(a); printRef(b); printPtr(&a); printPtr(&b); return 0; } ---- output class a class a class a class b class a class b I seem that I don't understand elements of C++. Lastly, I thank you all again for answering my question. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
![]() |
| Thread Tools | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| iostream thorwing constructors | usenet | c++ | 0 | 09-13-2007 08:55 AM |
| istream, ostream and iostream | usenet | c++ | 1 | 08-21-2007 03:12 AM |
| iostream cin problem | usenet | c++ | 9 | 08-20-2007 03:47 PM |
| <iostream> query | usenet | c++ | 5 | 10-07-2006 12:46 AM |
| iostream it's a joke right? | usenet | c++ | 55 | 10-01-2006 05:58 PM |


