Typecast Mistake

I probably showed you the wrong syntax for a programmer-defined type-cast yesterday. When I got back to the office and tried it out, the thing I naturally typed wasn't right, and that suggests very strongly that the thing I would have written on the board in response to a surprise question would also have been wrong.

This is how it should have looked. It makes a special kind of integer called "Int" instead of "int", which makes all forms of assignment completely traceable. The typecast operator (which must not be given a return type, is called int (or whatever type you are defining a cast into), and has no parameters) is used in nearly all contexts where an Int appears and the system already knows how to make use of an int.
      #include <iostream>
      #include <string>
      
      class Int
      { protected:
          int val;
        public:
          Int(void) 
            { cout << "Creating uninitialised Int\n"; }
          Int(int v) 
            { cout << "Initialising an Int to " << v << "\n";
              val=v; }
          Int(const Int & v)
            { cout << "Initialising an Int to " << v.val << "\n";
              val=v.val; }
          Int & operator=(const Int & v)
            { cout << "Reassigning an Int to " << v.val << "\n";
              val=v.val;
              return *this; }
          Int & operator=(int v)
            { cout << "Reassigning an Int to " << v << "\n";
              val=v;
              return *this; }
          operator int(void)
            { return val; } };
      
      void main(void)
      { int x=32, z=21;
        Int yyy=99;
        cout << " --- step 1\n";
        x=yyy+2;
        cout << " --- step 2\n";
        yyy=x*z;
        cout << " --- step 3\n";
        cout << "yyy = " << yyy << "\n"; }
      
Initialising an Int to 99
 --- step 1
 --- step 2
Reassigning an Int to 2121
 --- step 3
yyy = 2121

    
A minor change that makes it even more useful is to be able to give the Ints names that can be printed as part of the testing.
      #include <iostream>
      #include <string>
      
      class Int
      { protected:
          int val;
          string name;
        public:
          Int(void) 
            { cout << "Creating uninitialised anonymous Int\n";
              name="anonymous"; }
          Int(string nm) 
            { cout << "Creating uninitialised Int called " << nm << "\n";
              name=nm; }
          Int(int v) 
            { cout << "Initialising an anonymous Int called to " << v << "\n";
              val=v;
              name="anonymous"; }
          Int(string nm, int v) 
            { cout << "Initialising an Int called " << nm << " to " << v << "\n";
              val=v;
              name=nm; }
          Int(const Int & v)
            { cout << "Initialising an anonymous Int to " << v.val << "\n";
              val=v.val;
              name="anonymous"; }
          Int(string nm, const Int & v)
            { cout << "Initialising an Int called " << nm << " to " << v.val << "\n";
              val=v.val;
              name=nm; }
          void setname(string nm)
            { cout << "Setting name of Int valued " << val << " to " << nm << "\n";
              name=nm; }
          Int & operator=(const Int & v)
            { cout << "Reassigning an Int called " << name << " to " << v.val << "\n";
              val=v.val;
              return *this; }
          Int & operator=(int v)
            { cout << "Reassigning an Int called " << name << " to " << v << "\n";
              val=v;
              return *this; }
          operator int(void)
            { return val; } };
      
      void main(void)
      { Int x=32, z("z",21);
        Int yyy("yyy", 99);
        x.setname("x");
        cout << " --- step 1\n";
        x=yyy+2;
        cout << " --- step 2\n";
        yyy=x*z;
        cout << " --- step 3\n";
        cout << "yyy = " << yyy << "\n"; }

        
Initialising an Int called z to 21
Initialising an Int called yyy to 99
Setting name of Int valued 32 to x
 --- step 1
Reassigning an Int called x to 101
 --- step 2
Reassigning an Int called yyy to 2121
 --- step 3
yyy = 2121