I love using smart pointers, and have seen a bit of code which makes nice use of typedef
s 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.
In C++, Forward declarations are usually used for Classes. In this, the class is pre-defined before its use so that it can be called and used by other classes that are defined before this. Example: // Forward Declaration class A class A; // Definition of class A class A{ // Body };
In Objective-C, classes and protocols can be forward-declared like this: @class MyClass; @protocol MyProtocol; In Objective-C, classes and protocols can be forward-declared if you only need to use them as part of an object pointer type, e.g. MyClass * or id<MyProtocol>.
But you can't forward declare a typedef. Instead you have to redeclare the whole thing like so: typedef GenericValue<UTF8<char>, MemoryPoolAllocator<CrtAllocator> > Value; Ah, but I don't have any of those classes declared either.
That use of typedefs doesn't make it prettier- it's worthless. You can make a shared_ptr
to an incomplete type, so just use shared_ptr<B>
.
If you want to share typedefs you probably want to use namespaces for declaring them:
struct A;
struct B;
namespace Aimpl {
typedef boost::shared_ptr<A> pointer;
}
namespace Bimpl {
typedef boost::shared_ptr<B> pointer;
}
struct A {
Bimpl::pointer b_;
};
struct B {
Aimpl::pointer a_;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With