Virtual functions and inheritance
via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 27 01:54:56 PST 2015
On Tuesday, 27 January 2015 at 08:19:46 UTC, Daniel Kozák wrote:
> You can use this T:
>
> class Parent {
> @property string typeName(this T)() {
> return T.stringof;
> }
> }
>
> class Child : Parent {
> }
>
> void main() {
> auto p = new Parent;
> auto c = new Child;
>
> assert(p.typeName == "Parent");
> assert(c.typeName == "Child");
> }
OTOH:
void main() {
auto p = new Parent;
auto c = new Child;
assert(p.typeName == "Parent");
assert(c.typeName == "Child");
p = c;
assert(p.typeName == "Parent");
}
It will still use the static type of the object that it's called
on, not the dynamic type.
More information about the Digitalmars-d-learn
mailing list