Call parent method from child, similar to calling "super" (C ++)

Is it possible to "reuse" a parent method in a child method and add functionality just like with the superoperator in Java?

parent.method(int a){
  a++;
}

child.method(int a /*would be nice w/out*/){
  printf("%d",a);
}

      

I know this is probably a pretty simple question, sorry about that.

I know I can just copy / paste the method into the child class and add functionality there by overloading; However, I am looking for a more convenient way.

+3


source to share


4 answers


You can use __ super for this, but this is a Microsoft extension.

void CChild::function( int nParam )
{
    __super::function( nParam );
}

      



Alternatively, you can explicitly call the base implementation from the derived class:

void CChild::function( int nParam )
{
    CParent::function( nParam );
}

      

+6


source


You can call a parent member function in a child member function by assigning the name of the member function to the name of the parent class:



void child::method()
{
    parent::method();
}

      

+6


source


Yes, the keyword is super

missing, but instead you just specify the name of the parent class.

int child::method(int a ){ 
  // call base class
  int i = parent::method(a);
  printf("%d",a); 
} 

      

+4


source


parent.method(int a){
  a++;
}

child::method(int a /*would be nice w/out*/){
  printf("%d",a);
  parent::method(a);
}

      

+1


source







All Articles