Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. virtual functions

virtual functions

Scheduled Pinned Locked Moved Design and Architecture
questiondesignregexarchitectureannouncement
4 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tom groezer
    wrote on last edited by
    #1

    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.

    P 1 Reply Last reply
    0
    • T tom groezer

      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.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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.

      N 1 Reply Last reply
      0
      • P Pete OHanlon

        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.

        N Offline
        N Offline
        nitikin
        wrote on last edited by
        #3

        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

        P 1 Reply Last reply
        0
        • N nitikin

          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

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups