By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,948 Members | 1,545 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,948 IT Pros & Developers. It's quick & easy.

Recompilation problem

P: n/a
Hi all,

I have a situation something like this

Class A
{
private data
......
public functions
f1();
f2();
f3();
}

There is another application which uses this class

Class B
{
#include a.h
A objofA;
........
........
}

If I make any changes to the implementation of class A, I need to
recompile the class B. How do I ensure that even if I do any changes to
class A, I don't need to recompile class B. If there any facility
provided by C++ for that?

Sep 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ar********@yahoo.co.in wrote:
Hi all,

I have a situation something like this

Class A
{
private data
......
public functions
f1();
f2();
f3();
}

There is another application which uses this class

Class B
{
#include a.h
A objofA;
........
........
}

If I make any changes to the implementation of class A, I need to
recompile the class B. How do I ensure that even if I do any changes to
class A, I don't need to recompile class B. If there any facility
provided by C++ for that?


Yes, it has a strange name. It's called the pimpl idiom (pimpl strands
for pointer to implementation, who makes this up?).

The idea is to use a pointer to the A object in your B class. Now there
are lots of reasons not to do this, it's less efficient and it makes
coding the B class more complex, but if your overriding concern is to
avoid recompilation then it works.

Like this

class A; // forward declaration

class B
{
A* ptrofA;
};

By using a forward declaration of class A you avoid having to #include
"a.h" in b.h, so there is no recompilation when a.h changes.

john
Sep 18 '05 #2

P: n/a
Hi John,

Thanx a lot for the reply.

I am curious to understand how the compiler takes care of this
situation.
Any idea how it is internally taken care of/implemented. Any pointers
in this regard are highly appreciated.

Thanx in Advance,

Regards,
Aruna

John Harrison wrote:
ar********@yahoo.co.in wrote:
Hi all,

I have a situation something like this

Class A
{
private data
......
public functions
f1();
f2();
f3();
}

There is another application which uses this class

Class B
{
#include a.h
A objofA;
........
........
}

If I make any changes to the implementation of class A, I need to
recompile the class B. How do I ensure that even if I do any changes to
class A, I don't need to recompile class B. If there any facility
provided by C++ for that?


Yes, it has a strange name. It's called the pimpl idiom (pimpl strands
for pointer to implementation, who makes this up?).

The idea is to use a pointer to the A object in your B class. Now there
are lots of reasons not to do this, it's less efficient and it makes
coding the B class more complex, but if your overriding concern is to
avoid recompilation then it works.

Like this

class A; // forward declaration

class B
{
A* ptrofA;
};

By using a forward declaration of class A you avoid having to #include
"a.h" in b.h, so there is no recompilation when a.h changes.

john


Sep 19 '05 #3

P: n/a
ar********@yahoo.co.in wrote:

Hi John,

Thanx a lot for the reply.

I am curious to understand how the compiler takes care of this
situation.
Any idea how it is internally taken care of/implemented. Any pointers
in this regard are highly appreciated.


The key point is, that class B contains only a pointer
to another object. The compiler knows how big such a pointer
is going to be. It doesn't need to know the internals of class A
to determine this size information. Pointers to objects have always the
same size.

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.