Thread: What is this overload?

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    What is this overload?

    I have found the following code in the Internet. It works and the string receives "Yes". But this is not the operator overload syntax that I know.

    Thanks any help

    Code:
    #include <string>
    
    using namespace std;
    
    class T {
    public:
       operator string () { return "Yes"; }
    };
    
    int main() {
       string s;
       T t;
       
       s = t;
    
       return 0;  
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    That's overloading the typecasting to string. Basically, the assignment implicitly casts your T object to a string object before assigning it with the overloaded operator.
    Last edited by SlyMaelstrom; 07-02-2006 at 08:01 PM.
    Sent from my iPadŽ

  3. #3
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I understand, thanks so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overload *
    By shuo in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 04:44 AM
  2. Overload Operators
    By verbity in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2007, 11:13 AM
  3. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 03:03 PM
  4. C++ Operator Overload Question
    By cworld in forum C++ Programming
    Replies: 0
    Last Post: 03-25-2002, 07:17 PM
  5. Overload (+) operator
    By kit in forum C++ Programming
    Replies: 8
    Last Post: 10-23-2001, 11:20 AM