Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call base function then inherited function

Tags:

c#

inheritance

I have a base class and a class inheriting base. The base class has several virtual functions that the inherited class may override. However, the virtual functions in the base class has code that MUST to run before the inherited class overrides get called. Is there some way that I can call the base classes virtual functions first then the inherited class overrides. Without making a call to base.function().

I know I can simply make two functions, one that gets called, the other virtual. But is there a way I can keep the same names as well? I know I may need to change some things around.

class myBase {     public virtual myFunction()         { /* must-run code, Called first */ } }  class myInherited : myBase {     public override myFunction()         { /* don't use base.myFunction();,         called from base.myFunction(); */ } } 

Similar question here.

like image 714
Dave Avatar asked Sep 19 '10 22:09

Dave


People also ask

How do you call an inherited class function?

To have a derived function call a base function of the same name, simply do a normal function call, but prefix the function with the scope qualifier (the name of the base class and two colons). The following example redefines Derived::identify() so it first calls Base::identify() and then does its own additional stuff.

How do you call a derived function from base class?

You could do something like this: class Base { public: Base() {} virtual ~Base() {} virtual void start() { startInternal(); } virtual void stop() { stopInternal(); } void doSomething() { startInternal(); // ... stopInternal(); } private: void startInternal() { // ... }

Can be inherited by a derived class from a base class?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.

What is the relationship between base class and inherited class?

Question 3: What kind of relationship exists between inherited class and base class? Since the inherited class is a superset of the base class, there exists IS A relationship.


2 Answers

A common solution that can be found in the .NET Framework is to split a method in a public method XXX and a protected, virtual method OnXXX that is called by the public method. For your example, it would look like this:

class MyBase {     public void MyMethod()     {         // do something         OnMyMethod();         // do something     }      protected virtual void OnMyMethod()     {     } } 

and

class MyInherited : MyBase {     protected override void OnMyMethod()     {         // do something     } } 
like image 27
dtb Avatar answered Sep 19 '22 11:09

dtb


C# doesn't have support for automatically enforcing this, but you can enforce it by using the template method pattern. For example, imagine you had this code:

abstract class Animal {     public virtual void Speak()     {         Console.WriteLine("I'm an animal.");     } }  class Dog : Animal {     public override void Speak()     {         base.Speak();         Console.WriteLine("I'm a dog.");     } } 

The trouble here is that any class inheriting from Animal needs to call base.Speak(); to ensure the base behavior is executed. You can automatically enforce this by taking the following (slightly different) approach:

abstract class Animal {     public void Speak()     {         Console.WriteLine("I'm an animal.");         DoSpeak();     }      protected abstract void DoSpeak(); }  class Dog : Animal {     protected override void DoSpeak()     {         Console.WriteLine("I'm a dog.");     } } 

In this case, clients still only see the polymorphic Speak method, but the Animal.Speak behavior is guaranteed to execute. The problem is that if you have further inheritance (e.g. class Dachshund : Dog), you have to create yet another abstract method if you want Dog.Speak to be guaranteed to execute.

like image 129
Chris Schmich Avatar answered Sep 17 '22 11:09

Chris Schmich