Conversion Functions - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Conversion Functions

DonotaloDonotalo Posts: 715Member
[b][red]This message was edited by Donotalo at 2005-9-29 2:12:52[/red][/b][hr]
[code]
#include
using namespace std;

class Ratio {
int den, num;
public:
Ratio(int n = 0, int d = 1): den(d), num(n) {}
operator double() {return (double(den)/double(num));}
operator int() {return den/num;}
};

int main()
{
Ratio r(29, 15);
int i = r;
double d = r;

cout << i << endl << d << endl;
return 0;
}
[/code]
Output:
[code]
0
0.517241
[/code]
How does conversion functions return value where they cannot accept a return type?

Why they cannot be const member function?

Comments

  • bilderbikkelbilderbikkel Posts: 754Member
    : How does conversion functions return value where they cannot accept a return type?
    Could you rephrase this?

    : Why they cannot be const member function?
    They can! I've copied your code and made the functions const, as well as the member variables. What does your compiler give for an error message then?

    By the way, prefer new C++ style casts (that is, use static_cast) (see B.Stroustrup, 'The C++ Programming Language' paragraph 6.2.7 (3rd editition)).

    [code]
    #include
    using namespace std;

    class Ratio {
    const int den;
    const int num;
    public:
    Ratio(int n = 0, int d = 1): den(d), num(n) {}
    operator double() const { return static_cast(den)/static_cast(num); }
    operator int() const { return den/num; }
    };

    int main()
    {
    Ratio r(4, 27);
    int i = r;
    double d = r;

    cout << i << endl << d << endl;
    getchar();
    return 0;
    }
    [/code]

    Good luck,
    bilderbikkel

  • LundinLundin Posts: 3,711Member
    : [b][red]This message was edited by Donotalo at 2005-9-29 2:12:52[/red][/b][hr]
    : [code]
    : #include
    : using namespace std;
    :
    : class Ratio {
    : int den, num;
    : public:
    : Ratio(int n = 0, int d = 1): den(d), num(n) {}
    : operator double() {return (double(den)/double(num));}
    : operator int() {return den/num;}
    : };
    :
    : int main()
    : {
    : Ratio r(29, 15);
    : int i = r;
    : double d = r;
    :
    : cout << i << endl << d << endl;
    : return 0;
    : }
    : [/code]
    : Output:
    : [code]
    : 0
    : 0.517241
    : [/code]
    : How does conversion functions return value where they cannot accept a return type?

    [blue]I think it is just yet another case of weird C++ syntax. If you type operator int(), it only make sence for it to return int, so no return type is needed.
    I think this syntax would more logical: int operator(), double operator(), but it is not allowed.[/blue]

    : Why they cannot be const member function?
    :

    [blue]I'm not sure what you mean with const member function? If you mean "const qualifier", ie static, the reason is that they must have values to work with for them to make sence.

    In most cases, they should be typed as: operator int() const, sence it doesn't make sence to alter the object from it.
    [/blue]
  • DonotaloDonotalo Posts: 715Member
    [b][red]This message was edited by Donotalo at 2005-9-29 4:4:53[/red][/b][hr]
    : [red]I think this syntax would more logical: int operator(), double operator(), but it is not allowed.[/red]
    [purple]
    I agree with this. even if there is no return statement inside conversion function body, no error generated!

    now i know that conversion functions can be made const. i asked why it is not possible, because, my main program is much more different that this simple program. const conversion functions weren't allowed there, whereas it is allowed in this example. but here is a new problem:
    [code]
    #include
    using namespace std;

    class Ratio {
    int den, num;
    public:
    Ratio(int n = 0, int d = 1): den(d), num(n) {}
    operator double() [red]const[/red] {return (double(den)/double(num));}
    operator int() {return den/num;}
    };

    int main()
    {
    Ratio r(29, 15);
    int i = r;
    double d = r;

    cout << i << endl << d << endl;
    return 0;
    }
    [/code]
    Output: 0, new line, 0. why?? compiler VC++ 6.
    [/purple]
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [blue]Wny zero?

    Because you have this: double (den)
    And you need that: (double) den

    The parenthesis on a casting 'look the other way'! :-)
    [/blue]
  • LundinLundin Posts: 3,711Member
    : [b][red]This message was edited by Donotalo at 2005-9-29 4:4:53[/red][/b][hr]
    : : [red]I think this syntax would more logical: int operator(), double operator(), but it is not allowed.[/red]
    : [purple]
    : I agree with this. even if there is no return statement inside conversion function body, no error generated!
    :
    : now i know that conversion functions can be made const. i asked why it is not possible, because, my main program is much more different that this simple program. const conversion functions weren't allowed there, whereas it is allowed in this example. but here is a new problem:
    : [code]
    : #include
    : using namespace std;
    :
    : class Ratio {
    : int den, num;
    : public:
    : Ratio(int n = 0, int d = 1): den(d), num(n) {}
    : operator double() [red]const[/red] {return (double(den)/double(num));}
    : operator int() {return den/num;}
    : };
    :
    : int main()
    : {
    : Ratio r(29, 15);
    : int i = r;
    : double d = r;
    :
    : cout << i << endl << d << endl;
    : return 0;
    : }
    : [/code]
    : Output: 0, new line, 0. why?? compiler VC++ 6.
    : [/purple]
    :

    GCC gives a warning for it: "choosing operator int() because conversion sequence for the argument is better". It obviously chooses int-operator, probably because the object used when calling isn't const. If you put both operators to const, it will take the double one.

    It is probably because of priority with function overloading. If you have two member functions with the same name, like:

    [code]
    void hello() const
    {
    cout << "const";
    }

    void hello()
    {
    cout << "not const";
    }
    [/code]

    It will pick one depending on the type of the object.
    If the object isn't const, it will take the second one.
    If the object is const, only the first one makes sence.
Sign In or Register to comment.