Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++, typedefs and cross-header dependencies

Name: Anonymous 2007-02-23 14:19 ID:2IMTb0If

Imagine the following scenario:
There are two different classes (A and B) defined in two different headers (with include guards and all that crap). Out of convenience, shared pointers have been typedef'd for each class.
// a.h
struct A {
  typedef boost::shared_ptr<A> Ptr;
};

// b.h
struct B {
  typedef boost::shared_ptr<B> Ptr;
};

Now, if the classes wants to keep shared pointers to each other we'll run into problems (include guards fight). It's possible to solve this by either using raw pointers, moving the typedefs out of the classes or (preferably) remove one of the dependencies between the classes.

What I'm wondering if it's possible to solve some other way. Thing is, I usually put the Ptr-typedefs inside the class declarations because it's nifty to always be able to just use ClassName::Ptr to get a shared pointer.

I know that there's a fundamental issue here anyway with the circle reference but the issue of cross-dependency-of-typedefined-stuff still remains even if one pointer is replaced by a weak pointer.

In before forced indentation.

Name: Anonymous 2007-02-25 16:31 ID:yexl3zev

>>4
Some kind of forward declaration is required but I'm not sure how to accomplish it and keep the typedef inside the class scope.

class A;
typedef boost::shared_ptr<A> A::Ptr; // Error, A is still undefined.

I'm asking if it's possible to do this (in a pretty way). I know one could just move the typedef out of the class and call it APtr or similar but I'd like to avoid that if it's possible.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List