C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28630
Number of posts: 94612

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Conversion Functions Posted by Donotalo on 29 Sept 2005 at 2:06 AM
This message was edited by Donotalo at 2005-9-29 2:12:52

#include <iostream>
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;
}

Output:
0
0.517241

How does conversion functions return value where they cannot accept a return type?

Why they cannot be const member function?
Report
Re: Conversion Functions Posted by bilderbikkel on 29 Sept 2005 at 3:25 AM
: 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)).

#include <iostream>
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<double>(den)/static_cast<double>(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;
}


Good luck,
bilderbikkel

Report
Re: Conversion Functions Posted by Lundin on 29 Sept 2005 at 3:31 AM
: This message was edited by Donotalo at 2005-9-29 2:12:52

:
: #include <iostream>
: 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;
: }
: 

: Output:
:
: 0
: 0.517241
: 

: How does conversion functions return value where they cannot accept a return type?

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.


: Why they cannot be const member function?
:

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.

Report
Re: Conversion Functions Posted by Donotalo on 29 Sept 2005 at 4:04 AM
This message was edited by Donotalo at 2005-9-29 4:4:53

: I think this syntax would more logical: int operator(), double operator(), but it is not allowed.

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:
#include <iostream>
using namespace std;

class Ratio {
	int den, num;
public:
	Ratio(int n = 0, int d = 1): den(d), num(n) {}
	operator double() const {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;
}

Output: 0, new line, 0. why?? compiler VC++ 6.

Report
Re: Conversion Functions Posted by AsmGuru62 on 29 Sept 2005 at 4:34 AM
Wny zero?

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

The parenthesis on a casting 'look the other way'!

Report
Re: Conversion Functions Posted by Lundin on 29 Sept 2005 at 4:51 AM
: This message was edited by Donotalo at 2005-9-29 4:4:53

: : I think this syntax would more logical: int operator(), double operator(), but it is not allowed.
:
: 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:
:
: #include <iostream>
: using namespace std;
: 
: class Ratio {
: 	int den, num;
: public:
: 	Ratio(int n = 0, int d = 1): den(d), num(n) {}
: 	operator double() const {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;
: }
: 

: Output: 0, new line, 0. why?? compiler VC++ 6.
:

:

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:

void hello() const
{
  cout << "const";
}

void hello()
{
  cout << "not const";
}


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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.