I think the best way to avoid the XY problem problem is to give two answers. First, stating the short answer to the assumed question, then the long answer to the explicit question.
So, if somebody asks how they can access private member variables in C++, the first answer would say how to avoid needing to in the first place, and the second answer would say to buckle up because it’s time for the fun world of undefined-but-sometimes-useful behavior.
Given time as a limited resource, how much time do you want to spend on the long answer if it isn't going to help the person asking the question (assuming that the person asking the question isn't after an education and is more interested in "just solve my problem")?
Back when I was answering questions on one of the SE sites my rule of thumb for how much I was going to invest in answering a question was based on how much it appeared that the person invested in research and asking the question.
If they had only appeared to have spent a few minutes on asking the question and the answer was something that would have taken much longer to properly answer, I'd pass on answering and may only leave a comment asking for more information about the nature of the problem so that another person coming by later to answer it would not need to do as much research into providing an answer themselves.
So, if somebody asks how they can access private member variables in C++, the first answer would say how to avoid needing to in the first place, and the second answer would say to buckle up because it’s time for the fun world of undefined-but-sometimes-useful behavior.