www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - abstract bug? or my error?

reply Regan Heath <regan netwin.co.nz> writes:
--[bug.d]--
class A {
	abstract int x = 1;
	abstract static int y = 2;
	
	void foo() {
		printf("%d\n%d\n",x,y);
	}
}

class B : A {
	int x = 3;
	static int y = 4;
	
	void bar() {
		printf("%d\n%d\n",x,y);
	}
}

void main() {
	B b = new B();
	b.foo();
	b.bar();
}


D:\D\src\build\temp>dmd bug.d
d:\D\dmd\bin\..\..\dm\bin\link.exe bug,,,user32+kernel32/noi;

D:\D\src\build\temp>bug
1
2
3
4

Shouldn't this print?
3
4
3
4

Regan.

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 24 2004
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Regan Heath wrote:
 --[bug.d]--
 class A {
     abstract int x = 1;
     abstract static int y = 2;
     
     void foo() {
         printf("%d\n%d\n",x,y);
     }
 }
 
 class B : A {
     int x = 3;
     static int y = 4;
     
     void bar() {
         printf("%d\n%d\n",x,y);
     }
 }
<snip> I guess in D, just like (I think) C++ and Java, member variables cannot be overridden. And so x and y defined in B are distinct members, which hide the identically named members in A. And since they don't override, foo only sees the x and y declared in A, while bar sees the ones declared in B. I'm not convinced that this should be allowed, unless the base class x and y are private. But I guess it's debatable. But I would certainly consider it a bug that the word 'abstract' is allowed here. To specify new initialisations for inherited members, use a constructor. Static members (functions or variables) don't override either AIUI. If you need this for some type identification or similar, use a non-static property instead. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Jun 25 2004
next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
I should've said this is going to be my last post before I disappear off 
to Finland next week, if you hadn't picked up my post in digitalmars.D. 
  Still, maybe someone else here can help you....

Stewart.

-- 
My e-mail is valid but not my primary mailbox, aside from its being the 
unfortunate victim of intensive mail-bombing at the moment.  Please keep 
replies on the 'group where everyone may benefit.
Jun 25 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Fri, 25 Jun 2004 19:42:18 +0100, Stewart Gordon <smjg_1998 yahoo.com> 
wrote:

 I should've said this is going to be my last post before I disappear off 
 to Finland next week,
I feel honoured :)
 if you hadn't picked up my post in digitalmars.D.   Still, maybe someone 
 else here can help you....
Thanks. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 25 2004
prev sibling parent Regan Heath <regan netwin.co.nz> writes:
On Fri, 25 Jun 2004 19:28:31 +0100, Stewart Gordon <smjg_1998 yahoo.com> 
wrote:
 Regan Heath wrote:
 --[bug.d]--
 class A {
     abstract int x = 1;
     abstract static int y = 2;
         void foo() {
         printf("%d\n%d\n",x,y);
     }
 }

 class B : A {
     int x = 3;
     static int y = 4;
         void bar() {
         printf("%d\n%d\n",x,y);
     }
 }
<snip> I guess in D, just like (I think) C++ and Java, member variables cannot be overridden. And so x and y defined in B are distinct members, which hide the identically named members in A. And since they don't override, foo only sees the x and y declared in A, while bar sees the ones declared in B.
Ahh yes... I see there is no vtable for member variables only member functions. I can solve it by making a function for getting the variable. This occured to me just after posting this question.
 I'm not convinced that this should be allowed, unless the base class x 
 and y are private.  But I guess it's debatable.
In my actual problem they were private, but it seems to make no difference.
 But I would certainly consider it a bug that the word 'abstract' is 
 allowed here.
Yeah.. if this isn't to be allowed something like the C++ error: 'virtual' not permitted on data declarations should be given.
 To specify new initialisations for inherited members, use a constructor.
That's another way to solve it, so now I have 2 choices. Thanks.
 Static members (functions or variables) don't override either AIUI.  If 
 you need this for some type identification or similar, use a non-static 
 property instead.
This isn't what I need it for. RTTI would do this for me anyway, right? (I have not used it yet) Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 25 2004