K
Keith Willis
I've been beating my head against this for a while, so I'm now looking
for some hints and tips from the cognocenti...
I've been playing around with auto_ptr and as an exercise I've tried
to change a class of my own which uses the Pimpl idiom.
The original version is something like this:
//x.h
class MyClass
{
<snip stuff>
private:
class MyPrivate; // forward declaration
MyPrivate* p_;
};
//x.cc
// Constructor
MyClass::MyClass : p_(new MyPrivate()) {}
// Copy Constructor
MyClass::MyClass(const MyClass& that) : p_(new MyClass(*that.p_)) {}
// Assignment operator
MyClass&
MyClass:
perator=(const MyClass& rhs)
{
MyPrivate* t_ = new MyPrivate(*rhs.p_);
delete p_;
p_ = t_;
return *this;
}
The problem is I can't see for the life of me how to write the copy
constructor and assignment operator if I change the declaration of p_
to this:
//x.h
class MyClass
{
<snip>
private:
class MyPrivate; // forward declaration
std::auto_ptr<MyPrivate> p_;
};
Is there a 'canonical form' for expressing these?
TIA!
for some hints and tips from the cognocenti...
I've been playing around with auto_ptr and as an exercise I've tried
to change a class of my own which uses the Pimpl idiom.
The original version is something like this:
//x.h
class MyClass
{
<snip stuff>
private:
class MyPrivate; // forward declaration
MyPrivate* p_;
};
//x.cc
// Constructor
MyClass::MyClass : p_(new MyPrivate()) {}
// Copy Constructor
MyClass::MyClass(const MyClass& that) : p_(new MyClass(*that.p_)) {}
// Assignment operator
MyClass&
MyClass:
{
MyPrivate* t_ = new MyPrivate(*rhs.p_);
delete p_;
p_ = t_;
return *this;
}
The problem is I can't see for the life of me how to write the copy
constructor and assignment operator if I change the declaration of p_
to this:
//x.h
class MyClass
{
<snip>
private:
class MyPrivate; // forward declaration
std::auto_ptr<MyPrivate> p_;
};
Is there a 'canonical form' for expressing these?
TIA!