Mail Archives: djgpp/1997/06/10/15:47:44
>----------
>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 -