D - covariance in data members
- Carlos Santander B. (24/24) Sep 13 2003 Is there a chance this could ever work?
- Mike Wynn (39/63) Sep 13 2003 you will do the layout of D is
Is there a chance this could ever work?
class A {}
class B:A {}
class C {
A component;
A getComponent() { return component; }
}
class D:C {
B component;
}
void main() {
B b=new B;
D d=new D;
d.component=b; // 1
printf("%.*s\n",d.getComponent().classinfo.name); // 2
}
Put like this, I get an access violation in 2. If I change 1 to
d.C.component=b; then it works fine. But I don't want nor that neither to
override getComponent for each derivation of C.
-------------------------
Carlos Santander
---
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 2003-09-01
Sep 13 2003
Carlos Santander B. wrote:
Is there a chance this could ever work?
class A {}
class B:A {}
class C {
A component;
A getComponent() { return component; }
}
class D:C {
B component;
}
void main() {
B b=new B;
D d=new D;
d.component=b; // 1
printf("%.*s\n",d.getComponent().classinfo.name); // 2
}
Put like this, I get an access violation in 2. If I change 1 to
d.C.component=b; then it works fine. But I don't want nor that neither to
override getComponent for each derivation of C.
you will do the layout of D is
D{
C{
B{
A{
// a has no members
}
// B has no members
}
A component; // visible to C.getComponent
}
B companent; // new member (not change if the type of C.component;
it is this item that d.component refers to
}
allowing D to redefine the class of `component` would require a `write
barrier` in C to stop the following occuring
void func( C c, A a ) { c.componant = a; }
D d; func( d, new A() ); // would attempt to write an A to a B!
you can do ....
class C {
private A comp;
public void setComp( A c ) { comp = c; }
public A getComp() { return comp; }
}
class D:C {
public void setComp( A c )
in {
if ( c !== null ) {
B b = cast(B)c
assert( b );
}
}
body {
super.setComp( c );
}
public B getComp() { return cast(B)super.getComp(); }
}
Sep 13 2003








Mike Wynn <mike l8night.co.uk>