digitalmars.D - Overloading const member function
- Nub Public (11/11) Jun 23 2011 Why doesn't overloading of const member functions work?
- Daniel Gibson (3/17) Jun 23 2011 how do you want to call the different A() functions? i.e. how will D
- Nub Public (4/21) Jun 23 2011 It would depend on whether the instance of the struct is mutable or not.
- kenji hara (8/11) Jun 23 2011 In member function S.min, member function A is called from implicit
- kenji hara (5/14) Jun 23 2011 This is definitely bug of current dmd.
- Nub Public (2/22) Jun 23 2011
- Trass3r (11/22) Jun 23 2011 1)
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?
Jun 23 2011
Am 23.06.2011 10:11, schrieb Nub Public: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?how do you want to call the different A() functions? i.e. how will D know which one you want to call?
Jun 23 2011
On 6/23/2011 4:14 PM, Daniel Gibson wrote:Am 23.06.2011 10:11, schrieb Nub Public:It would depend on whether the instance of the struct is mutable or not. eg. the min() function is const, so the this object is const, and it would resolve to calling A() const.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?how do you want to call the different A() functions? i.e. how will D know which one you want to call?
Jun 23 2011
2011/6/23 Daniel Gibson <metalcaedes gmail.com>:how do you want to call the different A() functions? i.e. how will D know which one you want to call?In member function S.min, member function A is called from implicit 'this' variable typed const(S). Sois semantically analyzed to int min() const { return this.A() < this.b ? this.A() : this.b; } , then this.A() should be resolved overloads as int A() const. Kenji=A0 =A0 int min() const { return A() < b ? A() : b; }
Jun 23 2011
This is definitely bug of current dmd. It is already filed in bugzilla, see http://d.puremagic.com/issues/show_bug.cgi?id=3D1983 . Kenji 2011/6/23 Nub Public <nubpublic gmail.com>:Why doesn't overloading of const member functions work? struct S { =A0 =A0 =A0 =A0int a, b; =A0 =A0 =A0 =A0int A() const { return a; } =A0 =A0 =A0 =A0ref int A() { return a; } =A0 =A0 =A0 =A0int 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?
Jun 23 2011
I see. Thanks. On 6/23/2011 4:19 PM, kenji hara wrote:This is definitely bug of current dmd. It is already filed in bugzilla, see http://d.puremagic.com/issues/show_bug.cgi?id=1983 . Kenji 2011/6/23 Nub Public<nubpublic 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?
Jun 23 2011
Am 23.06.2011, 10:11 Uhr, schrieb Nub Public <nubpublic 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.
Jun 23 2011