472,325 Members | 1,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 software developers and data experts.

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 4737

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...
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...
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...
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<<...
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...
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: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.