Mail Archives: djgpp/1997/06/13/13:51:42
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 -