opCall example

Ary Borenszweig ary at esperanto.org.ar
Wed Jan 30 02:30:10 PST 2008


Saaa escribió:
> What does different exactly mean in this example?
> I read that s is of type S thus should be calling opCall (S v), but it 
> doesn't.
> 
> If opCall is overridden for the struct, and the struct is initialized with a 
> value that is of a different type, then the opCall operator is called:
> 
> struct S{   int a;
> 
>     static S opCall(int v)
>     {	S s;
> 	s.a = v;
> 	return s;
>     }
> 
>     static S opCall(S v)
>     {	S s;
> 	s.a = v.a + 1;
> 	return s;
>     }
> }
> 
> S s = 3;	// sets s.a to 3
> S t = s;	// sets t.a to 3, S.opCall(s) is not called

opCall is used like this:

S s = S(3);
S t = S(s);

I don't know why your first statement works. I think it's because S is 
just a wrapper for an int, so an int can be implicitly casted to S.


More information about the Digitalmars-d-learn mailing list