www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias this

reply "Mf_Gh" <Mf_Gh trash-mail.com> writes:
hi, i started to play a round with D and try to do something like:

class A{
	string a = "alias A";
	alias a this;
}

class B:A{
	string b = "alias B";
	alias b this;
}


void main() {
	A b = new B();
	writeln(b); //outputs alias A
}

i thought the output would be "alias B". why isn't the alias 
taken from the runtimetype?
Nov 22 2013
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, November 22, 2013 11:21:35 Mf_Gh wrote:
 hi, i started to play a round with D and try to do something like:
 
 class A{
 	string a = "alias A";
 	alias a this;
 }
 
 class B:A{
 	string b = "alias B";
 	alias b this;
 }
 
 
 void main() {
 	A b = new B();
 	writeln(b); //outputs alias A
 }
 
 i thought the output would be "alias B". why isn't the alias
 taken from the runtimetype?
Because there's no virtual function involved. There's nothing virtal about either a or b. They're just variables. You'd need to alias to a virtual function if you wanted polymorphic behavior. e.g. something like class A{ string a = "alias A"; string get() { return a; } alias get this; } class B:A{ string b = "alias B"; override string get() { return b; } alias get this; } void main() { A b = new B(); writeln(b); //outputs alias A } - Jonathan M Davis
Nov 22 2013
parent "Mf_Gh" <Mf_Gh trash-mail.com> writes:
thx
Nov 22 2013