Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
296 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

840 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