While CPallini's answer covers your immediate problem watch out for opening a big can of worms when you start using dynamic objects and raw pointers. For example Method1() is a resource leak waiting to happen:
void Method1()
{
pB = new B();
}
and you haven't got a destructor either.
So consider using one of two things:
- get rid of the pointer and just contain an instance of B. The class simplifies a lot as well:
class A
{
private:
B b_;
};
Er, that's it. You don't need to implement copy construction, copy assignment or a destructor.
- replace the pointer with a pointer like object, e.g. std::auto_ptr. This means you won't need a destructor and it simplifies your default constructor:
class A
{
public:
A( const A &a )
{
if( a.b_.get() ) b_.reset( new B( *b_ ) );
}
private:
std::auto_ptr<B> b_;
};
Personally I'd go for the straight member as it's a lot simpler and, unless there's a good reason for dynamically creating the contained object (like B is massive and not always created), doesn't buy you much.
Cheers,
Ash