home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 19020 < prev    next >
Encoding:
Text File  |  1993-01-12  |  1.2 KB  |  36 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!sunic!sics.se!eua.ericsson.se!euas62c36!euamts
  3. From: euamts@eua.ericsson.se (Mats Henricson)
  4. Subject: Re: Why X: :X(X &a), not X::X(X a) ?
  5. Message-ID: <1993Jan12.174308.9559@eua.ericsson.se>
  6. Sender: news@eua.ericsson.se
  7. Nntp-Posting-Host: euas62c36.eua.ericsson.se
  8. Reply-To: euamts@eua.ericsson.se
  9. Organization: Ellemtel Telecom Systems Labs, Stockholm, Sweden
  10. References: <1993Jan12.155159.22650@mobil.com>
  11. Date: Tue, 12 Jan 1993 17:43:08 GMT
  12. Lines: 22
  13.  
  14. In article 22650@mobil.com, winney@dal.mobil.com (Randy Winney) writes:
  15. #Could someone explain to us neophytes why C++ requires
  16. #that I use the constructor
  17. # X::X(X &a)
  18. #instead of simply
  19. # X::X(X a)
  20.  
  21. Because otherwise you'll end up with infinite recursion in this copy-
  22. constructor. To be able to get into the main code of X::X you need a
  23. copy of the argument, which will call the copy-constructor once again.
  24. To be able to get into the main code of this call to the copy-constructor,
  25. you need to make yet another copy of the argument, ... and so on...
  26. until you kill your program, or the second law of thermodynamics gets you.
  27.  
  28. I also suggest you declare the argument as const, i.e.:
  29.    X::X( const X& a );
  30.  
  31. Mats Henricson
  32. Ellemtel Telecom Systems Labs
  33. Stockholm
  34. Sweden
  35.  
  36.