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

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
klopterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tinchosCommented:
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
AlexFMCommented:
// 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
tinchosCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

fsign21Commented:
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
tinchosCommented:
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
_ys_Commented:
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
tinchosCommented:
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
_ys_Commented:
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
tinchosCommented:
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
_ys_Commented:
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
_ys_Commented:
>> Here is an untested code fragment which may make this more clear:
lol ... really.
0
tinchosCommented:
Yeap, I noticed that......... I guess it's inevitable
0
fsign21Commented:
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
klopterAuthor Commented:
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
tinchosCommented:
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
fsign21Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
klopterAuthor Commented:
fsign21

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

Ken
0
tinchosCommented:
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
klopterAuthor Commented:
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
tinchosCommented:
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
fsign21Commented:
ken,
I am glad, you found this technique helpful.
Have a nice weekend...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.