home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!utcsri!torn!news.ccs.queensu.ca!qucdn!0407658
- Organization: Queen's University at Kingston
- Date: Tue, 10 Nov 1992 10:41:03 EDT
- From: <0407658@QUCDN.QueensU.CA>
- Message-ID: <92315.1041030407658@QUCDN.QueensU.CA>
- Newsgroups: comp.lang.c++
- Subject: Constructor doesn't use overloading??
- Lines: 40
-
- Hello C++ Experts
- I am learning C++ (I'm up to page 320 of LaFore!) and I am surprised by a
- fact about constructors and overloaded assignment operators. Am I
- interpreting this correctly? I have a class called time which has a 3
- argument constructor with defaults:
-
- time(int hh=0, int mm=0, int ss=0) ; // constructor - defined later
-
- I also have an overloaded assignment operator which does a bit of
- housekeeping as shown below complete with debugging code:
-
- time & operator = (const time t)
- // assigns a time t to self AND converts it to proper format
- { cout << "\nI'm in = and t.second is " << t.second ; // debugging
- second = t.second % 60 ;
- cout << "second is " << second ; // debugging
- minute = (int(t.second / 60) + t.minute) % 60 ;
- hour = (int(t.minute / 60 ) + t.hour ) % 12 ;
- return * this ;
- }
- My surprise is that in main(), if I have:
-
- void main()
- { time t1(7, 5, 125) ;
- time t2(6,2) ;
- time t3 = t1 ;
- ...
- the overloaded = is not called; whereas if I change it to
- void main()
- { time t1(7, 5, 125) ;
- time t2(6,2) ;
- time t3 ;
- t3 = t1 ;
- then the = is called as expected.
-
- Could someone please enlighten this determined beginner as to why this
- is so and why its a good thing. I truly expected the overloaded = to be
- called in both cases. Thanks for your time, I really do appreciate it.
-
- Don
-