Operator overload

Discussion in 'C++' started by semut, Nov 22, 2006.

  1. semut

    semut Guest

    Hi, just like to find out when will this operator overload function
    gets triggered?


    Code Snippet

    class A {


    public:
    enum enA {
    A1, A2, A3
    } value;

    A();
    virtual ~A();
    A::enA& operator=(const UIScsiimInitReasonCode::enA& );
    operator A::enA() {
    return(value);
    };


    friend ostream& operator<< ( ostream&, const A&);
    };

    I know of the when will operator << and operator = be called, but I am
    not sure when will the
    operator A::enA() be called and what is the use (beside calling the
    operator function directly like
    a->operator A::enA()) ?

    thanks
     
    semut, Nov 22, 2006
    #1
    1. Advertising

  2. semut

    Ondra Holub Guest

    It is typecast operator. So it is called when instance of A is
    typecasted to A::enA (either implictly or explicitly).

    Look on following code:

    #include <iostream>

    class A
    {
    public:
    enum enA { A1, A2, A3 };

    A() { }
    ~A() { }

    operator A::enA() { return A2; }
    };

    void Fn(A::enA e)
    {
    std::cout << "Fn(A::enA)\n";
    }

    void Fn(...)
    {
    std::cout << "Fn(...)\n";
    }

    int main()
    {
    A a;
    Fn(a); // Will call Fn(A::enA) due to overload of operator A::enA()
    return 0;
    }

    There are 2 overloaded functions Fn. You can guess, which one is
    called, when parameter is instance of A.
     
    Ondra Holub, Nov 22, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr Smith
    Replies:
    7
    Views:
    474
    Mr Smith
    Feb 10, 2005
  2. Matthew Monopole

    overload operator -

    Matthew Monopole, Aug 8, 2003, in forum: C++
    Replies:
    1
    Views:
    482
    Jakob Bieling
    Aug 8, 2003
  3. Jakob Bieling

    Q: Overload operator new/delete

    Jakob Bieling, Aug 8, 2003, in forum: C++
    Replies:
    0
    Views:
    405
    Jakob Bieling
    Aug 8, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    377
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    259
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page