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

Pages: 1-

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-23 17:25 ID:XP9YP/2b

Use PImpl

Name: Anonymous 2007-02-25 14:02 ID:yexl3zev

I fail to see how pimpl will help.
To clarify:

// a.h
struct A {
  typedef boost::shared_ptr<A> Ptr;
  void SomeFunction(B::Ptr ptr);
};

// b.h
struct B {
  typedef boost::shared_ptr<B> Ptr;
  void SomeFunction(A::Ptr ptr);
};

No amount of PImpl can hide the function parameters.

Name: Anonymous 2007-02-25 14:36 ID:bHZEH2Y5

>>1
i do not quite understand what you are trying to accomplish, so i'll just assume proper application of forward declaration will solve all your problems.

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.

Name: Anonymous 2007-02-25 16:44 ID:d7i4ifzM

Too bad Ruby on Rails is slow as fuck.

Name: Anonymous 2007-02-25 18:25 ID:WvnwqTvp

>>1
Use a namespace.

Name: Anonymous 2007-02-25 18:33 ID:PcyhUb6H

>>1
>>5
What you're asking is impossible. Either move the typedef outside of the class or keep it inside and get errors

Name: Anonymous 2010-06-25 14:52

WARNING: NECRO POST

Name: Anonymous 2011-02-03 6:21

Name: Sgt.Kabu孃阭kimanꜚ젙 2012-05-28 22:47

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

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