home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!mcsun!sunic!sics.se!eua.ericsson.se!euas62c36!euamts
- From: euamts@eua.ericsson.se (Mats Henricson)
- Subject: Re: Why X: :X(X &a), not X::X(X a) ?
- Message-ID: <1993Jan12.174308.9559@eua.ericsson.se>
- Sender: news@eua.ericsson.se
- Nntp-Posting-Host: euas62c36.eua.ericsson.se
- Reply-To: euamts@eua.ericsson.se
- Organization: Ellemtel Telecom Systems Labs, Stockholm, Sweden
- References: <1993Jan12.155159.22650@mobil.com>
- Date: Tue, 12 Jan 1993 17:43:08 GMT
- Lines: 22
-
- In article 22650@mobil.com, winney@dal.mobil.com (Randy Winney) writes:
- #Could someone explain to us neophytes why C++ requires
- #that I use the constructor
- # X::X(X &a)
- #instead of simply
- # X::X(X a)
-
- Because otherwise you'll end up with infinite recursion in this copy-
- constructor. To be able to get into the main code of X::X you need a
- copy of the argument, which will call the copy-constructor once again.
- To be able to get into the main code of this call to the copy-constructor,
- you need to make yet another copy of the argument, ... and so on...
- until you kill your program, or the second law of thermodynamics gets you.
-
- I also suggest you declare the argument as const, i.e.:
- X::X( const X& a );
-
- Mats Henricson
- Ellemtel Telecom Systems Labs
- Stockholm
- Sweden
-
-