Firstly, this is a bad construct with delete ptr_b; in the class A destructor, since the constructor of A. does not have new B() . This means that each time an instance of A is created, you transfer ownership of B to A, leaving you with the potential risk of duplicate delete for those who use A, who do not know the internal components.
Secondly, if you want to give A a "stub" (or "mock") or a "fake" object instead of a "real B", B and FakeB need a common interface containing all the methods from B that A needs as virtual methods:
class FakeB : public InterfaceB
and
class B : public InterfaceB
therefore, all member functions from A can use parameters of type InterfaceB * instead of B * . Then injecting the FakeB object into A becomes clearly easy.
Unfortunately, this means you need to change B (at least a little). If this is not an option, it is always possible to wrap B with some WrapperB class (this is basically the same idea as in the classic Adapter Template ):
class WrapperB: public InterfaceB { B _b; public: WrapperB() : _b(){}
WrapperB will contain only very simple, simple code for delegating methods, for which you can omit unit tests. And you should use WrapperB instead of B when you are going to use it in combination with A. But what you get is ideal for testing class A
Another (possibly even better) option creates the WrapperB class in such a way that you enter a reference to object B from outside into it:
class WrapperB: public InterfaceB { B& _b; public: WrapperB(B& b) :_b(b){}
You can use it like this:
B b; A a(WrapperB(b)); FakeB fb; A a_for_test(fb);