Q:

C++: cannot access protected member declared:

I have a very strange problem:
class A
{
protected:
    A();
    ~A();
};

class B
{
public:
    void setA(A a)
    {
    }
};

The above code report error:
C2248: A::~A cannot access protected member declared in class 'A'

I can not understand why it need to access the ~A function in the declaration setA() ?
And, how can I fix this bug?

A:

In your class B member function, void setA(A a), the argument (a class A object) is passed by value - thus, a copy is made when the function is called, and that copy has to be destroyed - so the (protected) destructor will need to be called.
One way to fix the problem is to pass that A object by reference, using void setA(A& a) as the signature.

