Because of circular dependencies, you use forward declarations for some classes:
//B.h
class A;
class B
{
public:
void foo(A* a);
};
typedef SmartPtr<B> BPtr;
//A.h
class B;
class A
{
public:
void foo(B* b);
};
typedef SmartPtr<A> APtr;
Now, let’s say I want to change the prototypes of the functions to use smart pointers:
void A::foo(BPtr b);
void B::foo(APtr a);
Obviously, I can’t forward declare APtr or BPtr. What works is re-using the typedef:
//B.h
class A;
typedef SmartPtr<A> APtr;
class B
{
public:
void foo(APtr a);
};
typedef SmartPtr<A> APtr;
//A.h
class B;
typedef SmartPtr<B> BPtr;
class A
{
public:
void foo(BPtr b);
};
typedef SmartPtr<A> APtr;
but I’m not sure if this is the right solution. Is there a standard way of doing this? Is what I did wrong or dangerous?
Assuming you’ve got a good reason for these cyclical dependencies, why not do: