-
March 7th, 2006, 06:22 PM
#1
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.
-
March 7th, 2006, 08:05 PM
#2
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?
-
March 7th, 2006, 08:15 PM
#3
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
-
March 7th, 2006, 08:24 PM
#4
Re: Polymorphism .. override
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.
-
March 8th, 2006, 12:32 AM
#5
Re: Polymorphism .. override
Originally Posted by Mattrang
Can virtual be protected ?
If protected, then no override is allowed
-
March 8th, 2006, 12:47 AM
#6
Re: Polymorphism .. override
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 Derive2: Derive1{
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
-
March 8th, 2006, 01:03 AM
#7
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?
}
-
March 8th, 2006, 01:43 AM
#8
Re: Polymorphism .. override
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
-
March 8th, 2006, 01:48 AM
#9
Re: Polymorphism .. override
Thanks
So i don't need it ....
-
March 8th, 2006, 03:14 PM
#10
Re: Polymorphism .. override
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|