I have to register an object in a container upon its creation. Without smart pointers I\'d use something like this:
a_class::a_class()
{
register_somewhere(t
There is no need for shared_ptr in your code (as you show it and explain it). A shared_ptr is only appropriate for shared ownership.
Your b_class doesn't own it's a_class, in fact is even outlived by it, so it should merely keep an observing pointer.
If b_class is polymorphic and the manipulations of a_class involve altering its b_class pointer, you should use a unique_ptr:
class a_class;
class b_class
{
friend class a_class;
a_class* mya;
b_class(a_class*p)
: mya(p) {}
public:
virtual~b_class() {} // required for unique_ptr to work
virtual void fiddle(); // do something to mya
};
class a_class
{
std::unique_ptr myb;
public:
a_class()
: myb(new b_class(this)) {}
template
void change_myb()
{
myb.reset(new B(this));
}
};