Error calling function and passing a reference-to-pointer with a derived type

c++, pointers, polymorphism, reference

Solution

Imagine you could do that. The reference is not const, so it's possible for DoSomething to assign to the pointer and that will be visible in the caller. In particular, inside DoSomething it's possible for us to change the pointer to point to something that isn't an instance of Derived. If the caller then tries to do Derived-specific things to the pointer after we return, it'll explode.

Problem

Can somebody explain why the following code is not valid? Is it because the offset for the variable named `d` is different than the variable named `b`? ``` class Base { public: int foo; }; class Derived : public Base { public: int bar; }; int DoSomething( Base*& b ) { return b->foo; } Base* b = new Derived; Derived* d = new Derived; int main() { DoSomething( d ); } ``` This is the error that the online Comeau C++ compiler gives: ``` "ComeauTest.c", line 12: error: a reference of type "Base *&" (not const-qualified) cannot be initialized with a value of type "Derived *" DoSomething( d ); ^ ``` This is a similar question but is different because in my example, I am declaring `d` as a pointer type: Passing references to pointers in C++ Note that this does compile when I pass `b` to `DoSomething`.

Original source

Related problems