473,473 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

boost::shared_ptr mutual header inclusion problem

Hi

I've the following header problem that I need two classes to know each
other through a boost::shared_ptr. Does any of you smart guys have a
solution?

A.h
----------------------
#include "B.h"

Class A
{
public:
typedef boost::shared_ptr< A > SharedPtr;
void foo( B::SharedPtr& _b) {};
private:
B::SharedPtr b;

}

B.h
-------------------------
#include "A.h"

Class B
{
public:
typedef boost::shared_ptr< B > SharedPtr;
void foo( A::SharedPtr& _a) {};
private:
A::SharedPtr a;
}

Best Regards
Dennis Nielsen

Jan 5 '06 #1
2 4802

krema2ren wrote:
Hi

I've the following header problem that I need two classes to know each
other through a boost::shared_ptr. Does any of you smart guys have a
solution?

A.h
----------------------
#include "B.h"

Class A
{
public:
typedef boost::shared_ptr< A > SharedPtr;
void foo( B::SharedPtr& _b) {};
private:
B::SharedPtr b;

}

B.h
-------------------------
#include "A.h"

Class B
{
public:
typedef boost::shared_ptr< B > SharedPtr;
void foo( A::SharedPtr& _a) {};
private:
A::SharedPtr a;
}


Don't use the typedef, and instead use forward class declaration, and
remove the #include

class A; //forward class declaration

class B
{
public:
void foo( boost::shared_ptr< A >& _a);// {};//Should move
implementation to source file for this method
private:
boost::shared_ptr< A > a;
};

FYI:
You should avoid using underscore prefix for your variable names,
because it can make your code non-portable. IAW C++ standard, variable
names that begin with an underscore are reserved for the
implementation.

Jan 5 '06 #2

Axter wrote:
Don't use the typedef, and instead use forward class declaration, and
remove the #include

class A; //forward class declaration

class B
{
public:
void foo( boost::shared_ptr< A >& _a);// {};//Should move
implementation to source file for this method
private:
boost::shared_ptr< A > a;
};


You can also get around the problem by adding a fwd.h header for your
classes. You can also write your own forward class for boost
shared_ptr. Thus:

// boost_spfwd.h
namespace boost
{
template< typename T > shared_ptr;
tempalte < typename T > scoped_ptr;
}

..// afwd.h

#include boost_spfwd.h
class A;
typedef boost::shared_ptr< A > A_Ptr;
typedef boost::shared_ptr< const A > A_CPtr;
typedef boost::scoped_ptr< const A > A_ScopedPtr;

// etc and choose your own naming convention.

Of course it goes without saying you use all the correct file guards.

Jan 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented...
5
by: ctick | last post by:
Are there any advantages of using boost::shared_ptr other than auto_ptr from standard library?
6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
2
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
5
by: Jun | last post by:
Hello, I've code like : =========================================== class A{ public : // create print content friend std::ostream& operator<< (std::ostream& os, const A& a);
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
by: phlip | last post by:
Nick Keighley wrote: CC'd to the correct newsgroup. Yes, the destructor of the shared pointer will delete the object. Then its former address will convert to a reference. At some point -...
10
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.