Keith Willis wrote:
I've been beating my head against this for a while, so I'm now looking
for some hints and tips from the cognocenti...
You haven't bang your head enough, cause your example suck. Anyway, take
a look at this:
#include <memory>
#include <iostream>
class MyClass
{
public:
MyClass(int a);
~MyClass();
MyClass(const MyClass& that);
MyClass& operator=(const MyClass& rhs);
void a() const;
private:
class MyPrivate; // forward declaration
std::auto_ptr< MyPrivate p_;
};
class MyClass::MyPrivate
{
public:
MyPrivate(){}
~MyPrivate(){}
int a;
};
//x.cc
// Constructor
MyClass::MyClass(int a) : p_(new MyPrivate)
{
p_->a=a;
}
MyClass::~MyClass()
{
}
// Copy Constructor
MyClass::MyClass(const MyClass& that)
{
this->p_.reset( new MyPrivate );
this->p_->a = that.p_->a;
}
// Assignment operator
MyClass& MyClass::operator=(const MyClass& rhs)
{
this->p_.reset( new MyPrivate );
this->p_->a = rhs.p_->a;
return *this;
}
void MyClass::a() const
{
std::cout<<p_->a<<std::endl;
}
int main()
{
MyClass a(2);
MyClass b(3);
a.a();
b.a();
a = b;
a.a();
b.a();
}