Overloading const member function

Trass3r un at known.com
Thu Jun 23 04:49:31 PDT 2011


Am 23.06.2011, 10:11 Uhr, schrieb Nub Public <nubpublic at gmail.com>:

> Why doesn't overloading of const member functions work?
>
> struct S {
> 	int a, b;
> 	
> 	int A() const { return a; }
> 	ref int A() { return a; }
> 	
> 	int min() const { return A() < b ? A() : b; }
> }
>
> DMD2 compiler error: S.A () is not callable using argument types () const
>
> Is it a bug or by design?

1)
This particular case is probably a bug.

2)
Your code is typical C++ style.
In D you would normally use 'void A(int x) { a = x; }' to define a setter.

3)
Structs in D are POD structs like in C. Thus all members are public by  
default and setters/getters are unusual.
If you really need to restrict access via a getter (x)or setter you have  
to declare a and b private.


More information about the Digitalmars-d mailing list