delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/13/13:51:42

From: Roberto Henriquez Laurent <shl24634 AT alumnos DOT inf-cr DOT uclm DOT es>
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: <c=US%a=_%p=Hassler_Communic%l=DAISY-970610194623Z-153 AT daisy DOT hcst DOT com>
NNTP-Posting-Host: est271.mag-cr.uclm.es
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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/
> >
> >



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019