View mode: basic / threaded / horizontal-split · Log in · Help
May 10, 2005
typedef-ed ctor
Suppose a class has ctor defined, then a typedef-ed class cannot use that ctor:
------------------------------------------------------------   
class A { public
this(int param) {} }    

typedef A B;    

#void foo() {    
#  A a = new
A(0);  // OK    
#  B b = new B(0);  // error: cannot implicitly convert
expression    
#                   // (new A (0)) of type typedef.A to B    
#}
------------------------------------------------------------   

by contrast,
if no ctor defined in A, then it works:
------------------------------------------------------------   
class A {}
typedef A B;    

void foo() {   
A a = new A();  //OK   
B b = new B();  //OK
}   
------------------------------------------------------------
May 12, 2005
Re: typedef-ed ctor
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

typedef@d.com schrieb am Tue, 10 May 2005 02:00:09 +0000 (UTC):
> Suppose a class has ctor defined, then a typedef-ed class cannot use that ctor:
> ------------------------------------------------------------   
> class A { public
> this(int param) {} }    
>  
> typedef A B;    
>  
> #void foo() {    
> #  A a = new
> A(0);  // OK    
> #  B b = new B(0);  // error: cannot implicitly convert
> expression    
> #                   // (new A (0)) of type typedef.A to B    
> #}
> ------------------------------------------------------------   
>  
> by contrast,
> if no ctor defined in A, then it works:
> ------------------------------------------------------------   
> class A {}
> typedef A B;    
>  
> void foo() {   
> A a = new A();  //OK   
> B b = new B();  //OK
> }   
> ------------------------------------------------------------   

This is illegal code: http://digitalmars.com/d/class.html#constructors
# If there is no constructor for a class, but there is a constructor for
# the base class, a default constructor of the form:
#	this() { }
#	
# is implicitly generated.

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFCgr903w+/yD4P9tIRAmD/AKDIMkyX2t4vBwebTvtYjGmEcfdLDACcCUxV
Z58F8YAyPXK17lDQxlEtVXg=
=l/k8
-----END PGP SIGNATURE-----
Top | Discussion index | About this forum | D home