QUESTION 1:
Why do I get a compile error invalid parameter list when I set the user defined literal a double in the parameter list, instead of a long double? Is it because these literals are designed for scientific notation & they expect there to be much bigger numbers at times & just force you to use long double? I never would have figured this out if I had to do this on my own & with the given compile error!
So that I cannot set it like this:
Temperature operator "" _F (double fahrenheit)
QUESTION 2:
I think I understand the order of operation/creation, is this it....
1) "Temperature k1 = 31.73_F;" first operator ""_F is called & makes it's own temporary Temperature object, sets its Kelvin value, returns that pass by value to object k1 & then the temp object gets destroyed.
2) Object k1 then gets created with the passed by value.
Also, the author used structs instead of a class, but wouldn't it be more efficient to make a class instead and not need to create that temp object?
1 2 3 4 5
|
Temperature& operator ""(long double fahrenheit)
{
this.Kelvin = ((fahrenheit + 459.67) * 5/9);
return *this;
}
|
or maybe this way that is even more efficient & that does not need to update ALL of the variables if the Temperature class had many different variables?
1 2 3 4
|
void operator ""(long double fahrenheit)
{
this.Kelvin = ((fahrenheit + 459.67) * 5/9);
}
|
QUESTION 3:
A follow up from the one right above. This is common syntax for prefix ++ operator:
1 2 3 4 5 6 7 8 9 10
|
Date& operator ++()
{
++day;
return *this;
}
void DisplayDate()
{
cout << month << "/" << day << "/" << year << endl;
}
|
But if I used the prefix ++ operator like this because it works and it is more efficient, will someone say something and is this a big NO, NO.....someone can say operator ++ is EXPECTED to return Date& ?????
1 2 3 4 5 6 7 8
|
void operator ++()
{
++day;
}
void DisplayDate()
{
cout << month << "/" << day << "/" << year << endl;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
using namespace std;
struct Temperature
{
double Kelvin;
Temperature (long double kelvin): Kelvin(kelvin){}
};
Temperature operator "" _C (long double celcius)
{
return Temperature(celcius + 273);
}
Temperature operator "" _F (long double fahrenheit)
{
//return Temperature ((fahrenheit-32) * 5/9 + 273);
return Temperature ((fahrenheit + 459.67) * 5/9);
}
int main()
{
Temperature k1 = 31.73_F;
Temperature k2 = 0.0_C;
cout << "k1 = " << k1.Kelvin << " Kelvin" << endl;
cout << "k2 = " << k2.Kelvin << " Kelvin" << endl;
return 0;
}
|