www.digitalmars.com         C & C++   DMDScript  

D - Calling Parent Methods

reply Benji Smith <dlanguage xxagg.com> writes:
Is there a way to call methods in a parent class? Even if the child
class overloads the method definition?

Take a look at this code:

class baseClass {
	public void doSomething(int x) {
		return x+1;
	}
}


class derivedClass: baseClass {
	public void doSomething(int x) {
		x *=2;
		return baseClass.doSomething(x);
	}
}

I haven't seen anything in the D spec that hints at this kind of
functionality. Is this currently possible, and I'm just not aware of
it? If it's not possible, is there any good reason why we couldn't add
this to the spec?
Aug 13 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
yes super.method( .... ); as in ...
------------------
import c.stdio;

class A {
 this() { printf("A::this()\n"); }
 void show() { printf("A::show()\n"); }
}

class B : A {
 this() { printf("B::this()\n"); }
 void show() { super.show(); printf("B::show()\n"); }
}

int main( char[][] args ) {
 B b = new B();
 b.show();
 return 0;
}


// output (as you would expect)
//A::this()
//B::this()
//A::show()
//B::show()


"Benji Smith" <dlanguage xxagg.com> wrote in message
news:qrikjvk30j82a8l5vfrhg8840v1sg4elsh 4ax.com...
 Is there a way to call methods in a parent class? Even if the child
 class overloads the method definition?

 Take a look at this code:

 class baseClass {
 public void doSomething(int x) {
 return x+1;
 }
 }


 class derivedClass: baseClass {
 public void doSomething(int x) {
 x *=2;
 return baseClass.doSomething(x);
 }
 }

 I haven't seen anything in the D spec that hints at this kind of
 functionality. Is this currently possible, and I'm just not aware of
 it? If it's not possible, is there any good reason why we couldn't add
 this to the spec?
Aug 13 2003
parent Benji Smith <dlanguage xxagg.com> writes:
Aha. Fantastic. This really ought to be documented on the
"functions.html" page or the "classes.html" page (which mentions using
"super" to call parent-class constructors, but not other methods) on
the digital mars website.

--Benji

On Wed, 13 Aug 2003 16:05:00 +0100, "Mike Wynn"
<mike.wynn l8night.co.uk> wrote:

yes super.method( .... ); as in ...
Aug 13 2003