Thank you both.
bithub, I understand what you say, except for the part where you say that one can use the implementation directly. No one will ever have implementation.h, simply because that is really hidden and previously compiled either as a library or as an object file. (AFAIK)
I don't, however, understand what changes one must do to the interface when using this technique. If we keep the implementation class backwards compatible, then the interface class will too, I think. What happens is that if we implement a new function, we have to do it everywhere, in the implementation and in the interface.
Interface.h
Code:
class Implementation; //Forward class declaration
class Interface {
Interface(int);
void setValue(int);
int getValue(void) const;
~Interface();
private:
Implementation* ptr;
};
Implementation.h
Code:
class Implementation {
int v;
public:
Implementation (int val) : v(val) { }
void setValue ( int val ) { v = val }
void getValue(void) const { return v; }
};
interface.cpp - Might be hidden from the programmer under a library or an object
Code:
Interface:.Interface ( const int val ) : ptr ( new Implementation(val) ) { }
void Interface::setValue(int val) { ptr->setValue( val ); }
int Interface::getValue() const { return ptr->getValue(); }
Interface::~Interface(void) { delete ptr; }
@anon: I had never thought about that advantage, and it sure makes sense. But a small part of what we gain in compile time, we lose in run-time speed, right?