From: Roberto Henriquez Laurent Newsgroups: comp.os.msdos.djgpp Subject: Re: copy constructor Date: Wed, 11 Jun 1997 10:12:51 +0200 Organization: Universidad de Castilla-La Mancha Lines: 64 Message-ID: <339E5E03.8D54602D@alumnos.inf-cr.uclm.es> References: NNTP-Posting-Host: est271.mag-cr.uclm.es Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Bryan Murphy wrote: > I had quite a bit of confusion with Copy Constructors and Constructors > > when I got back into C++ programming a few months ago (it had been > two years). Here is what I did. I seriously recommend you do this, > not > only will it answer your question, but it will help you visualize > exactly > what is going on: (not tested, but should work) > > class CLOWN > { > public: > CLOWN(char *name) > { > cout << "Constructor Called:" << name << endl; > myname = new char[strlen(name)+1]; > strcpy(myname,name); > } > > CLOWN(CLOWN &bobo) > { > // Hi bobo > cout << "Copy Constructor: " << myname << endl; > > } > > ~CLOWN() > { > cout << "Destructor called: " << myname << endl; > } > private: > char *myname; > } > > (int)float((int)(char *)) main() > { > CLOWN bozo("bozo"); > bozo = new CLOWN("anak"); I think that's wrong (to assign an object variable a pointer value), i.e., for that sentence to work, bozo should be CLOWN*, not CLOWN. So maybe it should be bozo = CLOWN("anak"); //Would this work? > CLOWN bongo("bongo"); > bozo = bongo; > CLOWN *bingo = new CLOWN("bingo"); > delete bingo; > // etc. > }; > > > > >Bryan Murphy (aka Dalroth) > >Web Developer > >HCST, Inc. : http://www.hcst.com/ > >Home Page: http://www.hcst.com/~bryan/ > > > >