2012年2月20日 星期一

Type Conversions


Operator overloading comes into play in converting one type to other. You already have experience using two methods of conversions. First, you can use a cast to convert one primitive type to another. The second method simply involves constructors. A non-default constructor takes the arguments provided and creates a new-user defined object.

The next type of conversion to consider to converting a user-defined object into either a primitive or another user-defined object. This is accomplished using an operator () function, as illustrated in the next two examples.

Example 6-8 - Conversion of a user-defined type to a primitive type
1      // File: ex6-8.cpp
2      #include <iostream>
3      using namespace std;
4     
5      class B {
6          int b;
7        public:
8          B(int i) : b(i) {}
9          operator int() const;
10      };
11     
12      B::operator int() const {
13        cout << "* B:: operator int() called\n";
14        return b;
15      }
16     
17      int main() {
18        B eight(8);
19        cout << eight << endl;
20        cout << eight + 5 << endl;
21        cout << 5 + eight << endl;
22        cout << (eight > 3) << endl;
23        return 0;
24      }


******  Output  ******
* B:: operator int() called
8
* B:: operator int() called
13
* B:: operator int() called
13
* B:: operator int() called
1

What would happen if operator int() was not defined?

Example 6-9 - More Conversions of a user-defined type

1      // File: ex6-9.cpp - More Type Conversions
2     
3      #include <iostream>
4      #include <string>
5      using namespace std;
6     
7     
8      class Day;    // forward declaration
9     
10      class Number
11      {
12          int n;
13      public:
14        // Constructor
15          Number(int i = 0) : n(i) { cout << "Number(int) ctor called\n"; }
16     
17        // Conversion operators
18          operator int() const;
19          operator Day() const;
20      };
21     
22      Number::operator int() const
23      {
24          cout << "* Number::operator int() called\n";
25          return n;
26      }
27     
28      const string Days[7] =
29         {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday",
30          "Saturday"};
31     
32     
33      class Day
34      {
35          string dow;
36      public:
37          // Constructor
38          Day(int n = 0);
39     
40          // Conversion operator
41          operator Number() const;    // convert Day to Number
42     
43          // !operator prints dow
44          void operator!() { cout << "dow = " << dow << endl; }
45      };
46     
47      Day::Day(int index)
48      : dow(Days[index % 7])
49      {
50          cout << "Day(int) ctor called\n";
51      }
52      Day::operator Number() const
53      {
54          cout << "** Day:: operator Number() called\n";
55          for (int i = 0; i < 7; i++) if (dow == Days[i]) return Number(i);
56          return Number(0);
57      }
58     
59      Number::operator Day() const
60      {
61          cout << "* Number::operator Day() called\n";
62          return Day(n);
63      }
64     
65     
66      int main()
67      {
68          Number N1(65);
69          cout << "N1 = " << N1 << endl;
70     
71          Day d1(1);
72          !d1;
73     
74          //  Day d2(N1);        Why is this an ambiguity?
75     
76          Number N2(d1);
77          cout << "N2 = " << N2 << endl;
78     
79          !Day(Number(d1)+2);
80     
81          return 0;
82      }


******  Output  ******

Number(int) ctor called
* Number::operator int() called
N1 = 65
Day(int) ctor called
dow = Monday
** Day:: operator Number() called
Number(int) ctor called
* Number::operator int() called
N2 = 1
** Day:: operator Number() called
Number(int) ctor called
* Number::operator int() called
Day(int) ctor called
dow = Wednesday

Program Analysis

Line 8: Why is there a Day forward declaration?
Line 15: Describe the Number constructor.
Line 25: What are the options for a return from Number::operator int()?
Line 28: Why is Days const?  How are the Days array elements created?
Line 48: Why "index % 7" ?
Line 56: Why is return Number(0) there?  (Is it a good idea?)
Line 74: Why is this line commented out?

沒有留言:

張貼留言