virtual function

  • Thread starter Thread starter juli jul
  • Start date Start date
J

juli jul

Hello,
I have a function in base class and a lot of child classes of it. Couple
of them need an input argument in this function and all the other don't.
How can I do it using ovveride and virtual?
Thank you!
 
juli jul said:
Hello,
I have a function in base class and a lot of child classes of it. Couple
of them need an input argument in this function and all the other don't.
How can I do it using ovveride and virtual?
Thank you!

The problem is that they are really two different methods, the compiler
can't treat them the same as the callstack is different - even if they have
the same intent.

You could use overloading and virtuals to give a form of this

class Foo
{
public virtual void Bar()
{
Bar(0);
}
public virtual void Bar(int x)
{
}
}

class Baz : Foo
{
public override void Bar()
{
}
}

class Quux : Foo
{
public override void Bar( int x )
{
}
}

The key thing is how does the base class know whether to call the version
with no params or the one with params?
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
It depends on whether you want to be able to call both versions of the
overload from a base class reference or just the one without parameters. In
either case it's not difficult

Case 1:
public class Foo
{
public virtual void Bar() {...}
}
public class Foo2 : Foo
{
public override void Bar() {...}
public void Bar(int i) {...}
}

In this case we could call Bar() from a reference to Foo or Foo2 but I could
only call Bar(int i) from a reference to Foo2. This is the most common
scenario because you normally won't know what paramater(s) the overload in
the derived class will need when declaring the base class.

Case 2:
public class Foo
{
public virtual void Bar() {...}
public virtual void Bar(int i) {...}
}
public class Foo2 : Foo
{
public override void Bar() {...}
}
public class Foo3 : Foo
{
public override void Bar(int i) {...}
}
public class Foo4 : Foo
{
public override void Bar() {...}
public override void Bar(int i) {...}
}

In this case we declare both overloaded methods in the base class, then the
derived classes can choose to override 0, 1 or both of the methods.
 
Richard Blewett wrote:
[...snip...]
The key thing is how does the base class know whether to call the version
with no params or the one with params?
[...snip...]

Why should the base class know that ? The user of the subclass should be
responsible of knowing about the instance's class and act accordingly.
 
Michael Voss said:
Richard Blewett wrote:
[...snip...]
The key thing is how does the base class know whether to call the version
with no params or the one with params?
[...snip...]

Why should the base class know that ? The user of the subclass should be
responsible of knowing about the instance's class and act accordingly.

Yep, point taken - thats what comes of writing in haste. I guess the
question should be who decides what version of the method should be
invoked - should all derived classes be capable of having both versions of
the method called? Or should each have only one or the other? Or should the
one with the parameterized version also support the parameterless version
but not the other way round?

Without extra information exactly how to model it is not clear.
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
The problem is that I want that the function will be defined in base
class for most classes (and this is problematic because if I will
override I will need to declare it again in derived classes and I don't
want to)
How can I make it for example this way:

For Foo2,Foo3,Foo4 I want the function to be as it is in base class with
no override and for Foo5,Foo6 I want it to get some argument and to act
as the one in the base class.

How can I do this?
Thabk you very much!
 
juli jul said:
The problem is that I want that the function will be defined in base
class for most classes (and this is problematic because if I will
override I will need to declare it again in derived classes and I don't
want to)
How can I make it for example this way:

For Foo2,Foo3,Foo4 I want the function to be as it is in base class with
no override and for Foo5,Foo6 I want it to get some argument and to act
as the one in the base class.

How can I do this?
Thabk you very much!

How can it act as the base method if it takes a parameter?

You mean functionally it does the same thing as the base version with some
slight alteration based on the parameter?

Can the parameter version do its work before or after the base version has
run or does it affect what the base version itself would do?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
How about...

class Base
{
private int i;
public void Test()
{
_Test(i);
}
protected void _Test(int i)
{
System.Console.WriteLine("Base "+i);
}
public void TestDefer()
{
YourDefer(i);
}
public virtual void YourDefer(int i)
{
System.Console.WriteLine("Base Defer "+i);
}
}
class Derived : Base
{
public void Test(int i)
{
i += 1;
_Test(i);
}
public override void YourDefer(int i)
{
i += 1;
System.Console.WriteLine("Derived Defer "+i);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Derived d= new Derived();
d.Test();
d.Test(1);
d.TestDefer();
System.Console.ReadLine();
}
}

Regards,
Jeff
 
Back
Top