Thread: extending ostream

  1. #1
    ilear_01
    Guest

    extending ostream

    I have never tried to extend ostream before but now I must in order to easily switch between sending to cout and sending to a socket. Thus I have

    -----
    class SockStreamublic ostream{
    private:
    Server *server;
    public:
    explicit SockStream(Server *);
    SockStream& operator <<(char*);
    SockStream& operator <<(int );
    };
    -----

    The strange thing is that when I do

    ------
    Sockstream sout(server);
    if(!sout)
    cout << "Could not find sout" << endl;
    recvfrom(.....);
    sout << "hello\n";
    ------

    it gives me:
    ...
    bash$ ./cerver 8907
    Could not find sout
    ...

    but:
    ...
    bash$ ./client 127.0.0.1
    hello
    ....


    My point being that it creates an object that is apparently 0, yet then is able to use it for some reason. Is there any reason that extending ostream is not straight foward?


    QUESTION 2:
    how do I make it so that my SockStream can do:

    sout << "hello" << endl; // ie. with the endl

    I dont know anything about what endl is, it gives me this compiler error:

    no match for 'SockStream & << ostream & (&)(ostream &)'


    any idea?

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    endl is a special ostream which sends a newline and flushes the output. You've overloaded operator<< for char* and int; now you have to do it for ostream& too.

  3. #3
    ilear_01
    Guest
    Code:
    class SockStream:public ostream{
    private:
    	Server *server;
    public:
    	explicit SockStream(Server *);
       SockStream& operator <<(char*);
    	SockStream& operator <<(int );
       SockStream& operator <<(ostream &);
    };
    
    SockStream& SockStream::operator <<(ostream &o){
    	server->sendToClient("\n");
    }
    is this what you mean? cause this doesnt work...

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >My point being that it creates an object that is apparently 0, yet then is able to use it for some reason.

    are you calling the constructor for ostream or ios?

    >Is there any reason that extending ostream is not straight foward?

    if you look at the header files for ostream and ios you'll see why its not straightforward! It seems that all the writing in ostream is done through a streambuf object (at least in Microsofts version). So what you probably need to do is derive a SockStreamBuf object from streambuf (it doesnt necessarily have to do any buffering) and use that to construct the ostream object from the SockStream constructor. This way you will still have all of the functionality of ostream.

    >how do I make it so that my SockStream can do:

    That should come for free if you extend ostream correctly
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use <fstream> and ostream overloading together
    By mosu' in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2006, 01:06 PM
  2. istream and ostream not declared?
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2005, 01:02 AM
  3. ostream valid flag?
    By AH_Tze in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2004, 03:29 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. ostream and istream
    By xddxogm3 in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2003, 07:36 AM