class Parent{
};
class Child:
public Parent
{
}
void Func(Parent*& param)
{
}
Child* c=new Child;
Func(c); //error
Here's the reason why:
struct Parent {};
struct Child: Parent { int a; };
void Func(Parent*& param) { param = new Parent(); }
int main() {
Child* c = 0;
Func(c); // suppose this was allowed, and passed a reference to "c".
c->a; // oh dear. The purpose of a type system is to prevent this.
}
If you can change Func
to take Parent *const &
, that would be OK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With