virtual functions
-
What is the intent of calling a virtual function in thebase class from the its overwridden version in derived class? Does it in any way relate to the template design pattern. How can the same be achieved by agrreggation.
-
What is the intent of calling a virtual function in thebase class from the its overwridden version in derived class? Does it in any way relate to the template design pattern. How can the same be achieved by agrreggation.
You would normally do it if you needed to perform the original processing provided by the base method in your derived method. For instance, suppose you have the following really trivial classes
public class ClassA { public virtual void DoThis() { Console.WriteLine("Hello"); } } public class ClassB : ClassA { public override void DoThis() { base.DoThis(); Console.WriteLine("World"); } }
If you do the following:
ClassB myClass = new ClassB(); myClass.DoThis();
Hello will be written to the console, and then World on a new line.
Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.
-
You would normally do it if you needed to perform the original processing provided by the base method in your derived method. For instance, suppose you have the following really trivial classes
public class ClassA { public virtual void DoThis() { Console.WriteLine("Hello"); } } public class ClassB : ClassA { public override void DoThis() { base.DoThis(); Console.WriteLine("World"); } }
If you do the following:
ClassB myClass = new ClassB(); myClass.DoThis();
Hello will be written to the console, and then World on a new line.
Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.
Actually, cant agree with this. The purpose of virtual function is not to call the base function. You could do that anyways even without virtual specified. The actual usage of virtual comes into play for polymorphism. In the above example, if you were instantiating the object via :
ClassA myClass = new ClassB();//note variable type is ClassA myclass.DoThis();
Its the ClassB.DoThis which is going to get called. Effectively, letting you choose the functionality at runtime. Please correct me if I am wrong.namaste, Nitin Koshy http://devwaves.blogspot.com ...and I thought I knew
-
Actually, cant agree with this. The purpose of virtual function is not to call the base function. You could do that anyways even without virtual specified. The actual usage of virtual comes into play for polymorphism. In the above example, if you were instantiating the object via :
ClassA myClass = new ClassB();//note variable type is ClassA myclass.DoThis();
Its the ClassB.DoThis which is going to get called. Effectively, letting you choose the functionality at runtime. Please correct me if I am wrong.namaste, Nitin Koshy http://devwaves.blogspot.com ...and I thought I knew
If you read the OP, you will see that he asks: "What is the intent of calling a virtual function in thebase class from the its overwridden version in derived class?" The implementation that I show answers this question - granted I don't go into the practicalities of design and polymorphism, but I am only interested here in showing why you call the base method.
Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.