home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16029 < prev    next >
Encoding:
Text File  |  1992-11-10  |  1.7 KB  |  50 lines

  1. Path: sparky!uunet!utcsri!torn!news.ccs.queensu.ca!qucdn!0407658
  2. Organization: Queen's University at Kingston
  3. Date: Tue, 10 Nov 1992 10:41:03 EDT
  4. From: <0407658@QUCDN.QueensU.CA>
  5. Message-ID: <92315.1041030407658@QUCDN.QueensU.CA>
  6. Newsgroups: comp.lang.c++
  7. Subject: Constructor doesn't use overloading??
  8. Lines: 40
  9.  
  10. Hello C++ Experts
  11. I am learning C++ (I'm up to page 320 of LaFore!) and I am surprised by a
  12. fact about constructors and overloaded assignment operators.  Am I
  13. interpreting this correctly?  I have a class called time which has a 3
  14. argument constructor with defaults:
  15.  
  16.       time(int hh=0, int mm=0, int ss=0) ; // constructor - defined later
  17.  
  18. I also have an overloaded assignment operator which does a bit of
  19. housekeeping as shown below complete with debugging code:
  20.  
  21.       time & operator = (const time t)
  22.       // assigns a time t to self AND converts it to proper format
  23.       {   cout << "\nI'm in = and t.second is " << t.second ; // debugging
  24.            second = t.second % 60 ;
  25.            cout << "second is " << second ;                   // debugging
  26.            minute = (int(t.second / 60) + t.minute) % 60 ;
  27.            hour = (int(t.minute / 60 ) + t.hour ) % 12 ;
  28.            return * this ;
  29.       }
  30. My surprise is that in main(), if I have:
  31.  
  32.   void main()
  33.   {    time t1(7, 5, 125) ;
  34.        time t2(6,2) ;
  35.        time t3 = t1 ;
  36.        ...
  37. the overloaded = is not called;  whereas if I change it to
  38. void main()
  39. {    time t1(7, 5, 125) ;
  40.      time t2(6,2) ;
  41.      time t3 ;
  42.      t3 = t1 ;
  43. then the = is called as expected.
  44.  
  45. Could someone please enlighten this determined beginner as to why this
  46. is so and why its a good thing.  I truly expected the overloaded = to be
  47. called in both cases.  Thanks for your time, I really do appreciate it.
  48.  
  49. Don
  50.