www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Hidden get method for properties

reply sclytrack pi.be writes:
I've only been using D sinds June 2006, I stumbled accross this today.


Is this normal D behavior that int number() is no longer accessible from the
derived class while backupNumber still is? 

I can work around this, without problems. :-)

dmd v0.163




import std.stdio;


class BaseClass
{
protected:
int _number;
public:
int number()
{
return _number;
}

int backupNumber()
{
return _number;
}
}

class BaseDerived:BaseClass
{
public:
void number(int value)
{
_number = value + 1;
}
}


int main()
{
BaseDerived derived = new BaseDerived();
derived.number = 10;
writefln( derived.number );	//Error 

//main.d(35): function main.BaseDerived.number (int) does not match argument
types ()
//main.d(35): Error: expected 1 arguments, not 0
//main.d(35): voids have no value

return 0;
}

One day, I will have a real girlfriend.
Jul 19 2006
parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
sclytrack pi.be wrote:
 I've only been using D sinds June 2006, I stumbled accross this today.
 
 
 Is this normal D behavior that int number() is no longer accessible from the
 derived class while backupNumber still is? 
 
 I can work around this, without problems. :-)
 
 dmd v0.163

It's got to do with function overloading in the derived class. When you overload number in Derived, you lose visibility to the other number method(s). As for why is that? I don't know. Maybe to prevent possible confusion.
 
 
 
 
 import std.stdio;
 
 
 class BaseClass
 {
 protected:
 int _number;
 public:
 int number()
 {
 return _number;
 }
 
 int backupNumber()
 {
 return _number;
 }
 }
 
 class BaseDerived:BaseClass
 {
 public:
 void number(int value)
 {
 _number = value + 1;
 }
 }
 
 
 int main()
 {
 BaseDerived derived = new BaseDerived();
 derived.number = 10;
 writefln( derived.number );	//Error 
 
 //main.d(35): function main.BaseDerived.number (int) does not match argument
 types ()
 //main.d(35): Error: expected 1 arguments, not 0
 //main.d(35): voids have no value
 
 return 0;
 }
 

Jul 19 2006
next sibling parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
Hasan Aljudy wrote:

 
 
 sclytrack pi.be wrote:
 I've only been using D sinds June 2006, I stumbled accross this today.
 
 
 Is this normal D behavior that int number() is no longer accessible from
 the derived class while backupNumber still is?
 
 I can work around this, without problems. :-)
 
 dmd v0.163

It's got to do with function overloading in the derived class. When you overload number in Derived, you lose visibility to the other number method(s). As for why is that? I don't know. Maybe to prevent possible confusion.

It has to do with the overload rules in D, which are taken from C++. It turns out that the issues people find with this feature in D, isn't particularly present when using C++, so people are quite often confused with the current operation (which is quite opposite to how it works in Java). The solution in this case is to use alias to pull the symbol into the namespace of the subclass: alias BaseClass.number number;
Jul 19 2006
parent sclytrack pi.be writes:
In article <e9l7d5$1g4j$1 digitaldaemon.com>, Lars Ivar Igesund says...
Hasan Aljudy wrote:

 
 
 sclytrack pi.be wrote:
 I've only been using D sinds June 2006, I stumbled accross this today.
 
 
 Is this normal D behavior that int number() is no longer accessible from
 the derived class while backupNumber still is?
 
 I can work around this, without problems. :-)
 
 dmd v0.163

It's got to do with function overloading in the derived class. When you overload number in Derived, you lose visibility to the other number method(s). As for why is that? I don't know. Maybe to prevent possible confusion.

It has to do with the overload rules in D, which are taken from C++. It turns out that the issues people find with this feature in D, isn't particularly present when using C++, so people are quite often confused with the current operation (which is quite opposite to how it works in Java). The solution in this case is to use alias to pull the symbol into the namespace of the subclass: alias BaseClass.number number;

Yeah, I found it a bit confusing. I'm sure Walter has a reason for designing it this way, but is too busy to answer this. Thanks for your reply, Sclytrack.
Jul 19 2006
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Hasan Aljudy wrote:
 
 sclytrack pi.be wrote:
 I've only been using D sinds June 2006, I stumbled accross this today.
 
 Is this normal D behavior that int number() is no longer accessible 
 from the
 derived class while backupNumber still is?
 I can work around this, without problems. :-)
 
 dmd v0.163

It's got to do with function overloading in the derived class. When you overload number in Derived, you lose visibility to the other number method(s). As for why is that? I don't know. Maybe to prevent possible confusion.

It carries over from C++ days. AIUI the point is to avert a scenario whereby a base class can gain a new overload of the method name, and thereby cause a call to use this instead of your function in the derived class. See http://tinyurl.com/owwve Stewart.
Jul 19 2006