smart pointers, typedefs and forward declarations

Viewed 2490

I love using smart pointers, and have seen a bit of code which makes nice use of typedefs make make them prettier. For example:

struct A {
    typedef boost::shared_ptr<A> pointer;
};

allows me to write: A::pointer a(new A);

But I have hit a minor snag in my typedef happiness :-/, forward declarations...

So imagine this scenario:

struct B;
struct A {
    boost::shared_ptr<B> b_;
};

struct B {
    boost::shared_ptr<A> a_;
};

works well enough, but I'd love to clean that up a little. Unfortunately, this is doesn't work

struct B;
struct A {
    typedef boost::shared_ptr<A> pointer;
    B::pointer b_; // <-- error here
};

struct B {
    typedef boost::shared_ptr<B> pointer;
    A::pointer a_;
};

I understand why, at that point in the file, the compiler has no information that B in fact has a type in it named pointer.

I have a feeling that I am stuck using the old approach at least for some stuff, but I'd love to hear some clever ideas.

3 Answers
Related