Thread: As this code is confused?

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    119

    As this code is confused?

    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;
    
     }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, I'm not sure if you're asing a real question, but the operator unsigned short is so that you can turn Counter objects into primitive shorts.

    i.e.
    Code:
    short s;
    Counter c(5);
    s = c;
    I think the book's example could be improved by actually using short.

    Code:
    int theShort2 = theCtr;
    Statements like this will still work because Counter will use its unsigned short operator and then the result will be promoted to int. The type int is not really that specific, it can be just another way to write "long" or be a number with a different range. People who are tired of not knowing how big ints are #include <cstdint> and use uint16_t, int32_t types, and etc.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about this code regarding bits
    By ddang in forum C Programming
    Replies: 5
    Last Post: 01-16-2013, 12:33 AM
  2. Please help with this code, I am confused
    By rjh9136 in forum C++ Programming
    Replies: 6
    Last Post: 11-20-2011, 11:38 PM
  3. Confused with following code
    By SeriousTyro in forum C Programming
    Replies: 1
    Last Post: 08-25-2009, 10:37 AM
  4. I'm confused as to what exactly is going on in this code
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2002, 02:22 PM
  5. I am confused with this code that somebody gave me
    By face_master in forum C++ Programming
    Replies: 14
    Last Post: 11-16-2001, 01:43 AM

Tags for this Thread