This is from a solution I posted previously for almost exactly the same question. You have fewer members this time. My answer is essentially the same this time, too. The main difference between yours and mine is I route most of it through a Set method so that I avoid duplicating code. That makes debugging much easier.
Here is the revised answer :
class A
{
public:
int * a1 { nullptr };
A() {}
A( int x ) {
Set( x );
}
A( const A & other ) {
Set( * other.a1 );
}
~A() {
delete a1;
a1 = nullptr;
}
A & operator = ( const A & other )
{
if( this != & other )
{
Set( * other.a1 );
}
return *this;
}
void Set( int x ) {
if( a1 )
delete a1;
a1 = new int( x );
}
};
I think it is pointless (no pun intended) to pass a pointer to an integer as an argument if you are only going access its value so I have it passed by value, not address.