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
.