CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2006
    Posts
    69

    Polymorphism .. override

    When you override a virtual function of a base class .. do you have to call the base class method ? Always ?

    I've tested with Visual Studio .. as i type override .. it automatically call the base class virtual function.

    Why ???? is it like that ?

    I mean ... whatever the implementation of the virtual function of the base class .. that implementation will be called ... but why should i ? I want my own implementation to be used only ....

    Thanks,
    Last edited by GoDaddy; March 7th, 2006 at 06:32 PM.

  2. #2
    Join Date
    Feb 2006
    Posts
    69

    Re: Polymorphism .. override

    i guess i could take it off, but i've seen alot of examples doing it, calling the base method in the override implementation. But why?

  3. #3
    Join Date
    Oct 2005
    Location
    Just moved to New Mexico
    Posts
    53

    Re: Polymorphism .. override

    Can virtual be protected ?
    MY tv is not bilingual, has no button to switch the language, please make them all in English at night because some of us don't understand , thank you

  4. #4
    Join Date
    Feb 2006
    Posts
    69

    Re: Polymorphism .. override

    Quote Originally Posted by Mattrang
    Can virtual be protected ?
    In C# there's no such thing as access modifier on inheritance .. not like c++.

    I know in c++ youd do class A : public B

    but in C# its

    class A : B

    EDIT : OH sorry i think you meant if you can declare a virtual method as protected ? The answer to that is NO.

  5. #5
    Join Date
    Mar 2006
    Location
    Holand
    Posts
    6

    Re: Polymorphism .. override

    Quote Originally Posted by Mattrang
    Can virtual be protected ?
    If protected, then no override is allowed

  6. #6
    Join Date
    Mar 2006
    Location
    You can guess, but it'll be wrong
    Posts
    12

    Re: Polymorphism .. override

    Quote Originally Posted by GoDaddy
    When you override a virtual function of a base class .. do you have to call the base class method ? Always ?

    I've tested with Visual Studio .. as i type override .. it automatically call the base class virtual function.

    Why ???? is it like that ?

    I mean ... whatever the implementation of the virtual function of the base class .. that implementation will be called ... but why should i ? I want my own implementation to be used only ....

    Thanks,
    Your inheritance hierarchy might have some problem
    PHP Code:
    class Base{
        public 
    virtual void funfoo(){
            
    Console.WriteLine("funfoo Base");
        }
    }

    class 
    Derive1 Base{
        public 
    override void funfoo(){
            
    Console.WriteLine("funfoo Derive1");
        }
    }

    class 
    Derive2Derive1{
        public 
    override void funcfoo(){
            
    Console.WriteLine("funfoo Derive2");
        }
    }

    class 
    AfterAll{
        public static 
    void Main(string[] args)
        {
            
    Base b  = new Base();
            
    Base d1 = new Derive1();
            
    Base d2 = new Derive2();
                  
            
    b.funfoo(); //Only this one calls the base class
            
    d1.funfoo();
            
    d2.funfoo();
            
            
            
    Derive1 d01 = new Derive1();
            
    Derive2 d02 = new Derive2();
            
            
            
    d01.funfoo();
            
    d02.funfoo();
        }

    Last edited by Emiene; March 8th, 2006 at 01:19 AM.
    Emiene Vous

  7. #7
    Join Date
    Feb 2006
    Posts
    69

    Re: Polymorphism .. override

    I meant that when i try to override a virtual function of a base class, Visual studio will do a autocomplete like this

    Code:
    public override void foo()
    {
         base.foo() //This part is generated automatically by VS.NET .. but why???? do i needed?
    }

  8. #8
    Join Date
    Mar 2006
    Location
    You can guess, but it'll be wrong
    Posts
    12

    Red face Re: Polymorphism .. override

    Quote Originally Posted by GoDaddy
    I meant that when i try to override a virtual function of a base class, Visual studio will do a autocomplete like this

    Code:
    public override void foo()
    {
         base.foo() //This part is generated automatically by VS.NET .. but why???? do i needed?
    }
    Ohhh...,
    I guess it is based on the understanding of object architecture, MS people may think it is a base class ctor that should be auto-generated during coding stage to, at the same time, remind end-users or coders what is overriden. It may seem to be irrelevent to many people but such a feature doesn't bug the application anyway.
    Auto-generating code on coding editors is not new at all, but other people may offer you other ideas on how such code snip is auto-created if you still want to understand..
    Emiene Vous

  9. #9
    Join Date
    Feb 2006
    Posts
    69

    Re: Polymorphism .. override

    Thanks

    So i don't need it ....

  10. #10
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Polymorphism .. override

    Quote Originally Posted by Mattrang
    Can virtual be protected ?
    Yes it can!!!
    For example:
    Code:
    class Base
    {
    	protected virtual  void foo()
    	{
    		
    	}
    }
    
    class Derived:Base
    {
    	protected override  void foo()
    	{
    		base.foo ();
    	}
    
    }
    This code works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured