I've been programming "C in C++" for around 10 years, mainly using classes for religious data encapsulation, and have been mucking around with virtual functions recently, trying to replicate the *effect* of function pointers in C.
I've reviewed the forums and can't find anyone dealing with this permutation of my problem.
Consider this code:
class A {
public:
virtual void f() { printf("Base\n"); } ;
};
class B: public virtual A {
public:
void f() { printf("f1\n"); };
B() { printf("B\n"); };
};
class C: public virtual A, public virtual B {
public:
void f() { printf("f2\n"); };
C() { printf("C\n"); };
};
int main(int argc, char* argv[]) {
A *a1, *a2;
B *b = new B();
C *c = new C();
a1 = b;
a2 = c;
a1->f();
a2->f();
}
|
Basically B is built from A, and C is built from A and B. Virtual inheritance. I get what I want, which is for f() calls derived from A that are present in B and C to be local in scope.
But. Let's comment out the stuff in Main, and change the constructor in B to say B(short x) {}; and you will get a compiler error that no matching functions were found:
No matching function for call to B::B()
Candidates are B::B(short int)
B::B(const B&)
|
And this is on the line for the constructor for C!
C(short x) { printf("C\n"); };
|
But. If you make the corresponding changes to C, instead (C short x), no compiler error.
But. If you make the corresponding changes to both functions, compiler error (again at the C constructor definition).
Seriously confused. How do I use non-trivial constructors in B and C? If at all?