Why does this extremely simple operation not work?

Jonathan M Davis jmdavisProg at gmx.com
Tue Feb 12 10:15:53 PST 2013


On Tuesday, February 12, 2013 18:04:18 monarch_dodra wrote:
> You'd get the same behavior problem in C++. Where you can't pass
> a "Child**" when asking for a "Parent**". Long story short, if
> you could, you'd be able to place a parent instance inside a
> child instance, and mess everything up:
> 
> void myFunc(Parent* obj)
> {
> static Parent par;
> if(!par) par = new Parent();
> obj = ∥
> }
> void main() {
> Child myChild = new Child();
> myFunc(&myChild);
> //Here, myChild is a reference to a Parent => Type system broken
> }

Yeah. It's good to keep in mind that whenever you see a class referred to as a 
type, it's really referring to a reference to a class object, _not_ the class 
object itself, which is why &obj doesn't point to the class object but to its 
reference, and the reference doens't have a parent or child relationship with 
any classes - just the class itself has that.

- Jonathan M Davis


More information about the Digitalmars-d mailing list