one). It depends on the lifetime of the pointer to B. Make sure that when you call b-> getFooValue (); b must be a valid pointer.
I suggest using an initialization list, and if you are only reading the value of object B, although its pointer then turns it into a pointer to persistent data.
MyClassA::MyClassA(const B *bObj) : b(bObj) {}
2). While B is on the stack, you need to delete it, and if it is allocated for the heap, then it must be deleted by another owner, you will have a memory leak. You can use the smart pointer to get rid of the problem.
source share