question about copy constructor

copy the sample from somewhere.

Assuming inside another class A, with method

calcB(B b);

when I do

B* bb = new B();
calc(*bb);
delete bb;

when name inside bb is cleaned by destructor, who can clean the name in copy constructor? Is this a mem leak? what is the solution?
thanks



class B //With copy constructor
{
private:
char *name;
public:
B()
{
name = new char[20];
}
~B()
{
delete name[];
}
//Copy constructor
B(const B &b)
{
name = new char[20];
strcpy(name, b.name);
}
};
There is no memory leak:

1
2
3
4
5
6
7
8
9
10
11
12
void calcB(B b)  // copy constructor for 'b' called here
{
} // destructor for 'b' called here

///

int main()
{
  B* bb = new B();  // default ctor for 'bb' called here
  calc(*bb); // copy ctor and dtor called as per above function
  delete bb; // dtor for 'bb' called here
}
thanks. missed one piece.
Topic archived. No new replies allowed.