On gnu compiler:
If you try to initialize a reference to a pointer to a base type, (e.g. Foo*&), with a pointer to a derived type, eg Bar*, the compiler rejects it claiming inability to match prototypes.
Is this a gnu compiler bug or is there sanity behind its recalcitrance?
Microsoft compiler is OK with this im told (but havent tried myself)
For example, casting to the base type pointer prior to function call does not help. But creating a separate base class pointer and initializing it by direct cast on the derived class will work. Go figure!!
See below:
struct Foo {
int a;
double b;
};
struct Bar : public Foo {
double c;
};
void afunc(Foo *& e) {
e->a = 23;
e = NULL;
}
void cfunc(Foo *& e) {
e->b = 34.5;
e = NULL;
}
void testfunc() {
Foo foo;
Foo *pfoo = &foo;
afunc(pfoo); // works fine
Bar bar;
Foo *pbar = &bar;
cfunc(pbar); // works fine, but requires explicit Foo* pointer
// Bar *ppbar = &bar;
// cfunc((Foo *)ppbar); // compiler don’t like this, even with explicit cast
// I would expect
// cfunc(ppbar); to be ok, and certainly,
// cfunc((Foo *)ppbar); to be ok given that 6 lines above is ok by compiler
// can someone explain what I am missing please.
}