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
295 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
ID: 9614308
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
ID: 9614318
// 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
ID: 9614328
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Expert Comment

by:fsign21
ID: 9614329
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
ID: 9614334
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_
ID: 9614335
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
ID: 9614351
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_
ID: 9614423
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
ID: 9614458
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_
ID: 9614560
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
 
LVL 9

Expert Comment

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

Expert Comment

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

Expert Comment

by:fsign21
ID: 9614642
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
ID: 9614658
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
ID: 9614718
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
ID: 9614889
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
ID: 9615815
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
ID: 9616382
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
ID: 9616681
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
ID: 9616712
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
ID: 9617020
ken,
I am glad, you found this technique helpful.
Have a nice weekend...
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

777 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