Cyclic includes when using smart pointers.
Hey, I'm having trouble with cyclic includes in a project I'm working on. This is my first attempt at a project that heavily uses boost, and specifically, smart pointers. I have the following situation, simplified down. Header file 1: class Test1 { vector<Test2Ptr> mTest2Objects; }; typedef boost::shared_ptr<Test1> Test1Ptr; Header file 2: class Test2 { vector<Test1Ptr> mTest1Objects; }; typedef boost::shared_ptr<Test2> Test2Ptr; Now normally I'd get around this easily by doing a forward declaration, but I can't do that with a smart pointer definition. How can I work around this? So far I have found ways to work around the above(Since that's not precisely what I'm doing), but when I start inlining functions it will become very difficult to work around. Obviously something is wrong with my include structure or how I am going about this, but I don't know what... Any help? Is there a way I can forward-declare smart pointers? How are complicated cyclic includes usually resolved, if forward declares aren't enough(i.e., inline files)? Thanks, Jared
Jared Lee Richardson: ...
I have the following situation, simplified down.
Header file 1:
class Test1 { vector<Test2Ptr> mTest2Objects; };
typedef boost::shared_ptr<Test1> Test1Ptr;
Header file 2:
class Test2 { vector<Test1Ptr> mTest1Objects; };
typedef boost::shared_ptr<Test2> Test2Ptr;
Now normally I'd get around this easily by doing a forward declaration, but I can't do that with a smart pointer definition.
Why not? With raw pointers you'd do:
class Test2;
class Test1
{
vector
Oh ok, now I feel dumb. :(
I thought that because it was a template, I couldn't forward declare
it because the template would need information about the class.
I might still have some issues when I start inlining, but I should be
able to work around them.
Jared
On Tue, Mar 31, 2009 at 9:12 AM, Peter Dimov
Jared Lee Richardson: ...
I have the following situation, simplified down.
Header file 1:
class Test1 { vector<Test2Ptr> mTest2Objects; };
typedef boost::shared_ptr<Test1> Test1Ptr;
Header file 2:
class Test2 { vector<Test1Ptr> mTest1Objects; };
typedef boost::shared_ptr<Test2> Test2Ptr;
Now normally I'd get around this easily by doing a forward declaration, but I can't do that with a smart pointer definition.
Why not? With raw pointers you'd do:
class Test2;
class Test1 { vector
mTest2Objects; }; With shared_ptr you do
class Test2;
class Test1 { vector< shared_ptr<Test2> > mTest2Objects; };
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Jared Lee Richardson:
Oh ok, now I feel dumb. :(
I thought that because it was a template, I couldn't forward declare it because the template would need information about the class.
No need to feel dumb; most smart pointers do not support this use, but shared_ptr does. You can even do things like class X; shared_ptr<X> f( bool b, shared_ptr<X> p, shared_ptr<X> q ) { return b? p: q; }
participants (2)
-
Jared Lee Richardson
-
Peter Dimov