Recompilation problem

A

arunal2001

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?
 
J

John Harrison

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
 
A

arunal2001

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
 
K

Karl Heinz Buchegger

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top