Solved

How can I include "A.h" in B.h when compiling B.cpp but not require A.h for classes using B

Posted on 2003-10-24
21
292 Views
Last Modified: 2010-04-02
I have a class B which uses services from a class A.  Indeed, it contains private data of a type defined in A.h.  However, I don't want users of class B to have to include "A.h" (A.h is in a different directory and besides I would just prefer to hide that implementation detail from users of class B).



Here is an untested code fragment which may make this more clear:

A.h
  typedef int my_int ;

B.h
#include "A.h"

private:
my_int internal_data;



I would like to allow other classes to include B.h without having A.h in their include path.

Thanks,
  Ken
0
Comment
Question by:klopter
  • 9
  • 4
  • 4
  • +2
21 Comments
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
I don't think it would be possible to achieve what you're looking for.

if in B.h

you have a member of type my_int, then its definition must be there (this means A.h MUST be included)

if you want to avoid including A.h you could try to forward declare the my_int and to work with a pointer
but in order to do so, my_int should be a class........... It should be something like


A.h
class my_int
{
         // Class definition
};

B.h
class my_int;

private:
my_int * internal_data;

But I'm not sure if you can change that much..........

In your case, I would choose to leave the typedef and to include a.h

Don't know what's the problem with doing this
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
// B.h

class A;  // forward declaration

class B
{


...
    A* pA;
}

Include A.h to B.cpp only. Create new A in B constructor and destroy it in B destructor.
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
Sorry, maybe that last part is not clear enough, now I'm rereading it and I don't like the way it sounds

For what you told, I believe that the changes you have to make in order to avoid including A.h are so big that I guess that it's better to include A.h.

Hope this sounds better and it helps

Tincho

PS: The reason for the need to change my_int to a class is that you cannot forward declare typedefs.
0
 
LVL 1

Expert Comment

by:fsign21
Comment Utility
For all private member of class B, you could define a "private" class, where you place all private members

//file B.h

class B
{
private:
class BPrivate;
BPrivate* pBPrivate;
};

//file B.cpp
#include "A.h"
class B::BPrivate
{
public:
//private stuff
my_int internal_data;
};

//create BPrivate in constructor
B::B()
{
pBPrivate = new BPrivate();
}
//destroy it in destructor
B::~B()
{
delete pBPrivate;
}
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
Sorry alex, but I believe that the need to include A.h is that there is the typedef for my_int

and you cannot forward declare typedefs
0
 
LVL 9

Expert Comment

by:_ys_
Comment Utility
Have you tried externing it?

extern class ClassA;

This will generally work, until you actually require the definition for ClassA, i.e. ClassA.something or sizeof(ClassA) etc.

Bear in mind that compilation units (cpps) _will_ have to include A.h as sizeof(ClassA) and other definition details will be required.
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
Hey _ys_ maybe I got it wrong but I believe that the need to include A.h is that there is the typedef for my_int

0
 
LVL 9

Expert Comment

by:_ys_
Comment Utility
The question that klopter proposed, and the code he presented didn't match up. I chose to follow the text, which mentions classes A and B.

I agree that tpedef cannot be externed, that's why I did not present them as such.
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
Ok, maybe you're right

but as he posted just this


A.h
  typedef int my_int ;

B.h
#include "A.h"

private:
my_int internal_data;


where the only thing showing is my_int, so I thought that this was the important thing

I guess we'll have to wait for him to show up to see what the problem is
0
 
LVL 9

Expert Comment

by:_ys_
Comment Utility
I just realised that the keywork extern in my previous post is totally superfluous, as illustrated by what AlexFM posted. What was I thinking of.

tinchos, we're all right, as we all interpret things our own way. It will be interesting to see what klopter has to think. The first few posts overlap a lot, as eveyone posted between refreshes ... lol.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:_ys_
Comment Utility
>> Here is an untested code fragment which may make this more clear:
lol ... really.
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
Yeap, I noticed that......... I guess it's inevitable
0
 
LVL 1

Expert Comment

by:fsign21
Comment Utility
With the solution, which I posted, you definitely do not need to include A.h in your B.h class, regardless if my_int ist a class or typedef.

Actually you hide all your private members and could change them, without recompiling any of your client code, which uses the class B.
0
 

Author Comment

by:klopter
Comment Utility
Sorry about the confusion.  In this particular case, the A.h file is an C header file (not C++), and I have to surround the include with

extern "C" {
#include "A.h"
}

And, the typedef is a pointer to a struct, i.e.

A.h:
typedef A_struct*  A_ptr ;

B.h:

extern "C" {
#include "A.h"
}

private:
  A_ptr my_A_ptr;







One thing that I have considered is something like:

private:
   void *  my_A_ptr ;


while replacing all occurences of "my_A_ptr" in B.cpp with "(A_ptr) my_A_ptr"
or possibly some slightly cleaner variant of that.

Thanks,
  Ken


0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
2 things to keep in mind ken

C headers are valid in C++........... (remember C++ supports C code)

and the solution you posted would be correct, but why not doing something like
(I don't have a compiler at hand, so please check)


B.h:

struct AStruct;        // Forward declare the struct

typedef AStruct* A_ptr;

private:
  A_ptr my_A_ptr;

and there you go..........
0
 
LVL 1

Accepted Solution

by:
fsign21 earned 100 total points
Comment Utility
Eventually I found a nice description for the technique of hiding of the private implementation
Please have a look on
http://www.gamedev.net/reference/articles/article1794.asp

BTW, it works also with extern "C" includes
0
 

Author Comment

by:klopter
Comment Utility
fsign21

   That article looks to be exactly what I need.  I am implementing it right now.

Ken
0
 
LVL 9

Assisted Solution

by:tinchos
tinchos earned 50 total points
Comment Utility
Hey klopter

Did you read my post?

I believe that with the last post I placed, you don't have to include the A.h file, and it's just what you needed

Tincho
0
 

Author Comment

by:klopter
Comment Utility
Tincho,
  I like your solution because it is simpler, and simplicity is a virtue.
  Unfortunately, I couldn't figure out how to make it work because I was wrong in my earlier post that the typedef was a pointer to a struct.  The typedef (in A.h) is a struct, not a pointer to a struct.  I can't touch A.h and as you said, you cannot forward declare typedefs.

  Please accept my apologies for not getting my facts straight in my question.

fsign21,
  My implementation of your solution seems to be working fine.

  Thanks.
Ken
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
Ok, no problem ken

just another comment

if the typedef is a struct then you can do the following, which would be copy the typedef of the struct

B.h:

struct AStruct;        // Forward declare the struct

typedef AStruct A_ptr;

private:
  A_ptr my_A_ptr;

and there you go..........

yo don't have to include the header, just keep in mind that you have to mantaint the typedefs sincronized

Glad you solved it.

Tincho
0
 
LVL 1

Expert Comment

by:fsign21
Comment Utility
ken,
I am glad, you found this technique helpful.
Have a nice weekend...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now