delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/10/15:47:44

Message-ID: <c=US%a=_%p=Hassler_Communic%l=DAISY-970610194623Z-153@daisy.hcst.com>
From: Bryan Murphy <bryan DOT murphy AT hcst DOT com>
To: "'djgpp AT delorie DOT com'" <djgpp AT delorie DOT com>,
"'S2200253 AT nickel DOT laurentian DOT ca'" <S2200253 AT nickel DOT laurentian DOT ca>
Subject: RE: copy constructor
Date: Tue, 10 Jun 1997 15:46:23 -0400
MIME-Version: 1.0

>----------
>I'm trying to refine my understanding of the copy constructor and would
>like some clarification about it.
>
>Give a class called "Bozo".
>
>Bozo b; // creates a Bozo object
>Bozo anotherBozo(b);  // calls the copy constructor
>Bozo yetanotherBozo = b;  // does this also call the copy constructor?
>
>Also the Bozo copy constructor would be Bozo::Bozo(Bozo &)  Is this
>correct?

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");
	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