Thread: overloading operator

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    48

    overloading operator

    Long story short: i need to be able to define an operator with two different return types.
    Something like this that actually works:
    Code:
     
    void operator>>(char * szBuffer);
    bool operator>>(char * szBuffer);
    Code:
    44 D:\Dev-Cpp\..\tcp.h `bool TCPconnection_c::operator>>(char*)' and `void TCPconnection_c::operator>>(char*)' cannot be overloaded

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by like_no_other View Post
    Long story short: i need to be able to define an operator with two different return types.
    Something like this that actually works:
    Code:
     
    void operator>>(char * szBuffer);
    bool operator>>(char * szBuffer);
    Code:
    44 D:\Dev-Cpp\..\tcp.h `bool TCPconnection_c::operator>>(char*)' and `void TCPconnection_c::operator>>(char*)' cannot be overloaded
    Long story short: you can't do that. What is the use case you are attempting to solve? Chances are you are going about this the wrong way.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    I want to be able to call the operator in two ways:
    1. not checking if it succeded:
    Code:
    object>>pChar;
    2. checking a bool return to see if it worked:
    Code:
    if ( object>>pChar )
    {
     ..
    }
    Usefulness: rigurous error checking when needed, fast typing on tests/trivial uses - i'm writing some sort of stream object for a tcp connection.
    Last edited by like_no_other; 09-29-2009 at 01:07 PM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Both cases can be solved with the version that returns bool. In contexts where you don't care, you ignore it.

    At any rate, this sort of overloading is impossible. The return type is not a part of the function signature.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    C++ iostreams operators >> and << return a reference to the same stream to allow operator chaining. They also provide operator void* to make the stream object testable in boolean contexts (or you can google for "safe bool idiom" if you want to do better).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM