conversion operator question

Discussion in 'C++' started by hunter hou, Feb 10, 2006.

  1. hunter hou

    hunter hou Guest

    Hello,Please look at the following code(from C++ in a nutshell) and my
    questions.Thanks,***Hunter...
    typedef void (*strproc)(const char*);

    void print(const char* str)
    {
    std::cout << "const char*:" << str << '\n';
    }

    void print(int x)
    {
    std::cout << "int:" << x << '\n';
    }

    void print(double x)
    {
    std::cout << "double:" << x << '\n';
    }


    struct simple

    {
    void operator( )(int x) { print(x); } // print(int)
    void operator( )(double x) { print(x); } // print(double)
    };

    typedef void (*intfunc)(int);
    typedef void (*dblfunc)(double);

    struct indirect
    {
    operator intfunc( ) { return print; } // print(int)
    operator dblfunc( ) { return print; } // print(double)
    operator strproc( ) { return print; } // print(const char*)
    };



    int main( )
    {

    simple sim;
    indirect ind;


    sim(42); // Prints "int:42"

    sim.operator( )(42); // Prints "int:42"

    sim(42.0); // Prints "double:42"//sim(X) is using
    overloaded operator ()

    ind(42); // Prints "int:42"

    ind.operator intfunc( )(42); // Prints "int:42"

    ind(42.0); // Prints "double:42"

    ind("forty-two"); // Prints "const char*:forty-two"
    }Question:The above conversion coperators are used to convert indirect to
    intfunc, dblfunc,strproc, how does the above "ind" use coversion operators
    to do overloading?and how to decide parameter type?
    hunter hou, Feb 10, 2006
    #1
    1. Advertising

  2. * hunter hou:
    > Hello,Please look at the following code(from C++ in a nutshell) and my
    > questions.Thanks,***Hunter...
    > typedef void (*strproc)(const char*);
    >
    > void print(const char* str)
    > {
    > std::cout << "const char*:" << str << '\n';
    > }
    >
    > void print(int x)
    > {
    > std::cout << "int:" << x << '\n';
    > }
    >
    > void print(double x)
    > {
    > std::cout << "double:" << x << '\n';
    > }
    >
    >
    > struct simple
    >
    > {
    > void operator( )(int x) { print(x); } // print(int)
    > void operator( )(double x) { print(x); } // print(double)
    > };
    >
    > typedef void (*intfunc)(int);
    > typedef void (*dblfunc)(double);
    >
    > struct indirect
    > {
    > operator intfunc( ) { return print; } // print(int)
    > operator dblfunc( ) { return print; } // print(double)
    > operator strproc( ) { return print; } // print(const char*)
    > };
    >
    > int main( )
    > {
    >
    > simple sim;
    > indirect ind;
    >
    >
    > sim(42); // Prints "int:42"
    >
    > sim.operator( )(42); // Prints "int:42"
    >
    > sim(42.0); // Prints "double:42"
    > //sim(X) is using overloaded operator ()
    >
    > ind(42); // Prints "int:42"
    >
    > ind.operator intfunc( )(42); // Prints "int:42"
    >
    > ind(42.0); // Prints "double:42"
    >
    > ind("forty-two"); // Prints "const char*:forty-two"
    > }


    Be aware that this code is contrived to illustrate a technical aspect of
    the language.

    Nobody would write code like that to solve some actual problem.

    Most of us (including me) would have to compile it to be sure it's
    technically OK.



    Question:The above conversion coperators are used to convert indirect to
    > intfunc, dblfunc,strproc,


    Yes.

    > how does the above "ind" use coversion operators
    > to do overloading?


    One might argue that the conversion operators are overloaded on result
    type, which is no-no for ordinary functions. However, conversion
    operators are not ordinary functions. They are _special_ member
    functions, obeying special rules (other special member functions include
    constructors, destructors and assignment operators); special member
    functions are discussed in section 12 of the standard, "Special member
    functions", and conversion operators in 12.3.2.


    > and how to decide parameter type?


    That's something at least I don't want to think about; the rules for
    viable functions and matching are complex, to say the least. However,
    usually they produce the intuitive result, as they're designed to. Some
    times you get surprised, though, and then the best course is in my
    opinion to rewrite the code so that there is no (human-side) ambiguity.




    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Feb 10, 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. Bill97
    Replies:
    2
    Views:
    300
    Bill97
    Mar 11, 2005
  2. Arvid Requate
    Replies:
    2
    Views:
    983
    Alf P. Steinbach
    Jun 23, 2006
  3. hurcan solter
    Replies:
    3
    Views:
    730
    Cholo Lennon
    Aug 29, 2007
  4. , India
    Replies:
    2
    Views:
    461
    Fraser Ross
    Sep 15, 2009
  5. cronusf
    Replies:
    12
    Views:
    490
Loading...

Share This Page