昨天的一道面試題,分享下:
實現一個拷貝構造函數:
classFoo {
public:
Foo(A* a, B* b);
~Foo() {
delete a;
delete b;
}
// Implement copy constructor.
// Types A and B are Copy Constructible.
private:
A* a;
B* b;
};
答案:
Foo::Foo(const Foo& other)
{
this->a = new A(*other.a);
this->b = new B(*other.b);