Alex Rivera | Logout

What's your convention for typedef'ing shared_ptr?

Asked 2010-04-26T22:40:50.417
55

I'm flip-flopping between naming conventions for typedef'ing the boost::shared_ptr template. For example:

typedef boost::shared_ptr<Foo> FooPtr;

Before settling on a convention, I'd like to see what others use. What is your convention?

EDIT:

To those nesting the typedef inside Foo, doesn't it bother you that Foo is now "aware" of how it will be passed around? It seems to break encapsulation. How about this:

class Foo
{
public:
    typedef std::vector<Foo> Vector;
};

You wouldn't do this now, would you? :-)

Edit
Report

2 Answers

4

I'm generally not a fan of very short identifiers, but this is one case where I'll use them.

class Foo
{
public:
    typedef std::shared_ptr<Foo> p;
};

This enables shared_ptr to resemble ordinary pointers as closely as possible without risk of confusion.

Foo* myFoo;
Foo::p myFoo;

And as for breaking encapsulation—no, typedefing the shared_ptr type within the class doesn't break encapsulation any more than typedefing it outside of the class. What meaning of "encapsulation" would it violate? You're not revealing anything about the implementation of Foo. You're just defining a type. This is perfectly analogous to the relationship between Foo* and Foo. Foo* is a certain kind of pointer to Foo (the default kind, as it happens). Foo::p is another kind of pointer to Foo. You're not breaking encapsulation, you're just adding to the type system.

answered 2011-10-21T18:07:31.833
0
class foo;

typedef boost::shared_ptr<foo> foo_p;
typedef boost::weak_ptr<foo> foo_wp;
answered 2012-03-20T14:22:11.083

Your Answer