C
Christof Warlich
Hi,
I'm looking for a more concise way than below to extend an existing
interface.
Just imagine that class Interface would have 1000 other members and class
DerivedInterface only needs to add / modify _one_ member. The implementation
shall remain invisible to the application (i.e. main()).
Thanks for any suggestions,
Christof
#include <stdio.h>
class Interface {
public:
static Interface *create(void);
virtual void a(void) = 0;
virtual void b(void) = 0;
// and 1000 other pure virtual functions
};
class DerivedInterface: public Interface {
public:
DerivedInterface(void):
_base(Interface::create()) {
}
void a(void) {
printf("A\n");
}
void b(void) {
_base->b();
}
// and the 1000 other, unmodified functions
private:
Interface *_base;
};
int main(void) {
Interface *base = Interface::create();
base->a();
base->b();
Interface *derivedInterface = new DerivedInterface();
derivedInterface->a();
derivedInterface->b();
}
// everything below shall be kept invisible to the compilation unit
class Implementation: public Interface {
public:
void a(void) {
printf("a\n");
}
void b(void) {
printf("b\n");
}
};
Interface *Interface::create(void) {
return new Implementation;
}
I'm looking for a more concise way than below to extend an existing
interface.
Just imagine that class Interface would have 1000 other members and class
DerivedInterface only needs to add / modify _one_ member. The implementation
shall remain invisible to the application (i.e. main()).
Thanks for any suggestions,
Christof
#include <stdio.h>
class Interface {
public:
static Interface *create(void);
virtual void a(void) = 0;
virtual void b(void) = 0;
// and 1000 other pure virtual functions
};
class DerivedInterface: public Interface {
public:
DerivedInterface(void):
_base(Interface::create()) {
}
void a(void) {
printf("A\n");
}
void b(void) {
_base->b();
}
// and the 1000 other, unmodified functions
private:
Interface *_base;
};
int main(void) {
Interface *base = Interface::create();
base->a();
base->b();
Interface *derivedInterface = new DerivedInterface();
derivedInterface->a();
derivedInterface->b();
}
// everything below shall be kept invisible to the compilation unit
class Implementation: public Interface {
public:
void a(void) {
printf("a\n");
}
void b(void) {
printf("b\n");
}
};
Interface *Interface::create(void) {
return new Implementation;
}