[comp.lang.c++] Calling super methods

doug@zaphod.prime.com (08/10/89)

This is either a dumb question or a complaint.  If one has a derived class
then it is not uncommon to want to combine methods with a parent class.

So:

class base {
  int i;
  virtual int op();
};

class derived : base {
  int j;
  virtual int op();
};

int base::op()
{
  i++;
}

int derived::op()
{
  j++;
  // I want to call base::op here.  Do I have to explicitly
  // call:
  base::op();
  // ??
}

-------------------------------------------------------------------------------
Douglas Rand 
Internet:   doug@primerd.prime.com
Snail:	    Prime Computer, 500 Old Conn Path, MS10C-17, Framingham, Ma 01701
Disclaimer: PRIME doesn't believe a word I say, and fewer that I write.

sarima@gryphon.COM (Stan Friesen) (08/13/89)

In article <26200002@zaphod> doug@zaphod.prime.com writes:
>
>This is either a dumb question or a complaint.  If one has a derived class
>then it is not uncommon to want to combine methods with a parent class.
>
> [rest of example deleted]
>
>int derived::op()
>{
>  j++;
>  // I want to call base::op here.  Do I have to explicitly
>  // call:
>  base::op();
>  // ??
>}
>
Yes, either that or the following:

    base(*this).op();		// Cast the object to its parent class
-- 
Sarima Cardolandion			sarima@gryphon.CTS.COM
aka Stanley Friesen			rutgers!marque!gryphon!sarima
					Sherman Oaks, CA