As this code is confused? What is this code? An example of a very ambiguous. Even confusing. That operator unsigned short?? Where unsigned short? How it works if no variable is unsigned short?? Probably operator unsigned short means that the operator gives out, it gives out unsigned short? int (itsVal) that is, the type cast to int? itsVal already is int. The author got it wrong. I love these examples, they surprise you, make you think. You can cite various examples of their code, reload operators, showing good wise training examples?
Code:
// Листинг 10.17.
// Использование конструктора в качестве оператора преобразования типа
#include <iostream>
class Counter
{
public:
Counter();
Counter(int val);
~Counter(){ }
int GetItsVal()const { return itsVal; }
void SetItsVal(int x) { itsVal = x; }
operator unsigned short();
private:
int itsVal;
};
Counter::Counter():
itsVal(0)
{ }
Counter::Counter(int val):
itsVal(val)
{ }
Counter::operator unsigned short ()
{
return ( int (itsVal) );
}
int main()
{
int theShort = 5;
Counter theCtr = theShort;
std::cout << "theCtr: " << theCtr.GetItsVal() << std::endl;
int theShort2 = theCtr;
std::cout << "theShort: " << theShort << std::endl;
return 0;
}