Overload << operator.... sort of...

This is a discussion on Overload << operator.... sort of... within the C++ Programming forums, part of the General Programming Boards category; I'm new to operator overloading, and I don't know if this is possible. I want to create my own stream ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Overload << operator.... sort of...

    I'm new to operator overloading, and I don't know if this is possible. I want to create my own stream system to output debug specs to an edit control. I want it to function like this:
    Code:
    debugStream ds;
    
    ds << "Current Index: " << currIndex << endl;
    That code would result in that string, then that integer, then that string being sent to a function as parameters. Is this possible?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Yes you can do that. However you would have to basically write your own ostream class one that can handle all data types.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I have something now that works, but it's been basically all guess work.

    Code:
    HWND hEdit;
    
    class dStream
    {
    public:
    	dStream();
    	~dStream();
    	dStream operator << (char *);
    };
    
    dStream::dStream()
    {
    }
    
    dStream::~dStream()
    {
    }
    
    dStream dStream::operator <<(char *param)
    {
    	dStream ds;
    	DWORD size;
    	size=GetWindowTextLength(hEdit);
    	SendMessage(hEdit, EM_SETSEL,size,size);
    	SendMessage(hEdit,EM_REPLACESEL,(WPARAM)false, (LPARAM) param);
    	return ds;
    }
    
    .
    .
    .
    
    dStream ds;
    ds << "hello" << "\r\n";
    It works, but I dont understand some things. When I return an object from the overload function, what happens? At the moment, it's obviously not doing anything useful cause I'm just creating a dummy object. Would there be a use for returning an object though?

    Also, how can I enable sending endl to the stream?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You return a reference to the stream object to be used in calling the stream again in a row. Just like with cout.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    endl just outputs the '\n' character, then flushes the output buffer.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Originally posted by velius
    You return a reference to the stream object to be used in calling the stream again in a row. Just like with cout.
    Is this correct then?
    Code:
    dStream dStream::operator <<(char *param)
    {
    	DWORD size;
    	size=GetWindowTextLength(hEdit);
    	SendMessage(hEdit, EM_SETSEL,size,size);
    	SendMessage(hEdit, EM_REPLACESEL, (WPARAM) false, (LPARAM) param);
    	return *this;
    }
    endl just outputs the '\n' character, then flushes the output buffer.
    When I do that I get an error.
    Code:
    ds << "byebye" << std::endl;
    error C2679: binary '<<' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
    I understand that I may need to overload again, but in what way?
    Last edited by bennyandthejets; 10-23-2003 at 05:01 PM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    dStream dStream::operator <<(char *param)

    should be:

    dStream& dStream::operator <<(char *param)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to overload << in a templated Binary Tree
    By chinesepirate in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2008, 03:07 PM
  2. cout << SunTradingLLC << "is looking for C++ Software Developers" << endl;
    By sun trading in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 04-02-2008, 12:48 PM
  3. overload *
    By shuo in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 05:44 AM
  4. ambiguous << operator, overload not right?
    By neandrake in forum C++ Programming
    Replies: 7
    Last Post: 11-14-2004, 11:18 PM
  5. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 03:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21